0

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!

3 Answers 3

0

In Python2 use raw_input instead of input.

0
0

You should use the raw_input instead of input since raw_input will capture your input and convert it to the proper type. When using input you should add quotes around the input.

You can check this in the python docs: https://docs.python.org/2/library/functions.html#raw_input

0

As far as the code is concerned, the only change you need to make here is in server code. Replace c.close() with s.close() as c is a connection variable whereas s is the socket of server according to your code.

I have made your code run, after making the change it runs as expected.I executed it in Python 3.

The server code is here:

import socket

def Main():
    host = "127.0.0.1"       # supply different hostname instead of 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'))
    s.close()           # it is s which indicates socket

if __name__ == '__main__':
    Main()

And the client code is as given by you:

import socket

def Main():
# here, client is using the hostname whereas you need to give different 
# hostname for the server (127.0.0.1 for example) otherwise the code doesn't 
# work.You can do the reverse as well.
    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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.