I am trying to write a simple client-server program using python (not python3) and whenever I type a message to send it gives me various errors such as:
File "", line 1 hello my name is darp ^ SyntaxError: invalid syntax
OR
File "", line 1, in NameError: name 'hello' is not defined
OR
File "", line 1 hello world ^ SyntaxError: unexpected EOF while parsing
Here is the server code:
import socket
def Main():
host = socket.gethostname()
port = 5000
s = socket.socket()
s.bind((host, port))
s.listen(1)
c, addr = s.accept()
print("Connection from: "+str(addr))
while True:
data = c.recv(1024).decode('utf-8')
if not data:
break
print("From connected user: "+data)
data = data.upper()
print("Sending: "+data)
c.send(data.encode('utf-8'))
c.close()
if __name__ == '__main__':
Main()
AND here is the client code
import socket
def Main():
host = socket.gethostname()
port = 5000
s = socket.socket()
s.connect((host, port))
message = input("-> ")
while message != 'q':
s.send(message.encode('utf-8'))
data = s.recv(1024).decode('utf-8')
print("Recieved from server: " + data)
message = input("-> ")
s.close()
if __name__ == '__main__':
Main()
Even though I can create this connection, the problem occurs after I type the message. Any help would be appreciated, thanks!