-2

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

Sajal
  • 89
  • 1
  • 14

1 Answers1

1

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"
Agile_Eagle
  • 1,710
  • 3
  • 13
  • 32
  • Error i am getting now : `python: can't open file 'server.py': [Errno 2] No such file or directory`. Can you please tell me what is going wrong now. – Sajal Jul 03 '18 at 14:52
  • @sajal Did you enter full path of python file ? – Agile_Eagle Jul 03 '18 at 14:52
  • 1
    Your answer is slightly incorrect. You do not have to give the full path of the python file. Relative paths work just fine. – Bryan Oakley Jul 03 '18 at 14:53
  • Yes but since the file is not in current directory we need to input full path – Agile_Eagle Jul 03 '18 at 14:54
  • So first i need to create a file named `server.py` ? I am sorry but i am really new to this all. – Sajal Jul 03 '18 at 14:57
  • @Sajal if you haven't then how do you expect python to find server.py – Agile_Eagle Jul 03 '18 at 15:00
  • @Agile_Eagle Okay, now I typed in the terminal, python `C:/Users/Sajal Sirohi.DESKTOP-RTVGPUA/.PyCharmCE2018.1/config/scratches/server.py` _after creating the file server.py_ .. But it still is not working.. And the same error is coming. `python: can't open file 'C:/Users/Sajal': [Errno 2] No such file or directory` – Sajal Jul 03 '18 at 15:04
  • 1
    @Sajal add everything in double quotes then if it still does not work, add python before it – Agile_Eagle Jul 03 '18 at 15:06
  • @Agile_Eagle Okay thank you so much for helping and being patient with noobs like me. Thanks once again. – Sajal Jul 03 '18 at 15:11