Search code examples
pythonpython-3.xpython-socketspython-webbrowserpython-server-pages

Syntax error when trying to run server.py through command prompt


This is the code i am trying to run

import socket               


s = socket.socket()         
print ("Socket successfully created")

port = 12345                

s.bind(('', port))        
print ("socket binded to %s" %(port))

s.listen(5)     
print ("socket is listening")


while True:
   c, addr = s.accept()     
   print ('Got connection from', addr)
   c.send('Thank you for connecting')
   c.close()

OUTPUT This code is running successfully, but the problem occurs when i try to run these commands:

# start the server
$ python server.py
# keep the above terminal open
# now open another terminal and type:
$ telnet localhost 12345

Error that is coming: Error: Invalid syntax

When i type $ python server.py I am getting the same error. A possible solution to this would have been that i didnt set my environment variable right, but when i type python in the command prompt, i get no error. What I am doing wrong?

I copied this code from Here (Python socket network programming) PS: I am using 3.4.3 version


Solution

  • Python files have to run from the command prompt not from the shell

    Syntax for running python files is

    python "full path of python_file.py"