I'm having an AttributeError that says "AttributeError: 'socket' object has no attribute 'upper'". I'm pretty sure I created and established the connection properly, and am still unsure what to do after consulting the socket documentation.
Thank you.
import socket
from _thread import *
import threading
print_lock = threading.Lock()
list_of_clients = []
def threaded(data, addr, s):
s.sendto(data.upper(), addr)
while True:
message = s.recv(1024)
if not message:
print('Bye')
print_lock.release()
break
message = message.upper()
print("Sending message to " + addr[0])
s.sendto(message, addr)
data.close()
def Main():
list_of_clients = []
serverName = 'localhost'
serverPort = 12000
with socket.socket(socket.AF_INET , socket.SOCK_STREAM) as serverSocket:
serverSocket.connect((serverName, serverPort))
while True :
print('Ready to ping...')
data, addr = serverSocket.accept()
print(type(data))
print_lock.acquire()
print("Client connected ip:<" + str(addr) + ">")
start_new_thread(threaded, (data, addr, serverSocket))
print("Continue")
if __name__ == '__main__':
Main()
The problem is this line:
s.sendto(data.upper(), addr)
data
is defined here:
data, addr = serverSocket.accept()
The accept
method returns a 2-tuple, where the first element is a socket object capable of sending and receiving data.
Therefore, instead of passing serverSocket
to threaded
, you should pass the first element of the 2-tuple
, as well as whatever data you want to send.