0

I am working on a tcp client-server python socket program where I have written server code to sent a simple message to the client . However when I run the server side in python idle I get invlalid syntax error and a red mark on the python version . I don't know where the problem is and I would appreciate your help with this specific task . Image where error happens : enter image description here

I press run and then run module and I get : enter image description here

My code :

Server :

   import sys
   from socket import *
   serverSocket = socket(AF_INET,SOCK_STREAM)
   serverSocket.bind(('localhost',1234))
   serverSocket.listen()
   data = "Network labs"
   while 1 :
     connectionSocket ,addr = serverSocket.accept()
     connectionSocket.send(data)
     connectionSocket.close()

Client :

   import sys
   from socket import *
   clientSocket = socket(AF_INET,SOCK_STREAM)
   server_address=('localhost',1234)
   clientSocket.connect(server_address)
   sentence = clientSocket.recv(1024)
   print(sentence)
   clientSocket.close()
13
  • Your image doesn't show the syntax error. Could you replace it with one that does? It would also be helpful to type out the text of the syntax error message in another code block for easy readability. Commented May 24, 2020 at 20:25
  • @DavidZ it does . It is the red mark on top left on the python version 3.8.3 on the first 3 Commented May 24, 2020 at 20:29
  • That's not a syntax error though. I'm asking for the message that says SyntaxError and gives some information about what Python considers to have been the exact error, which is always printed by Python when you enter code with invalid syntax. Commented May 24, 2020 at 20:30
  • @DavidZ A window pops up saying "error invalid syntax" then it marks the 3 on the version Commented May 24, 2020 at 20:31
  • 1
    Have you tried to run it in a normal python REPL instead of the IDLE? Commented May 24, 2020 at 20:42

1 Answer 1

1

You tried to run the log of a shell session, complete with non-code startup message text and non-code prompts as python code. But the session log is not python code. "Python" might be, but "Python 3" is not valid code and so python reports a SyntaxError. This has nothing to do with running the code from an IDLE editor. If you run server.py from a command line or from any other python-aware editor or IDE, you would see the same.

To run server.py, you must remove the non-code parts -- the startup message and prompts. In general, you would also have to remove output, but there is none in your example. So you should end up with

import sys
from socked import *
...

In other words, the cleaned-up server code you listed in your question, which is not the code you ran in the screenshot to get the error message.

Sign up to request clarification or add additional context in comments.

3 Comments

I have . Now I get unexpected indent before import sys
As you posted the code, it has 3 spaces of indent (beyond the 4 spaces to signal 'code' to SO. If you run with any indent before 'import sys', you get that message.
I mean indents before 'while'. Your posted code has an extra 3 spaces of indent for every line that will prevent execution if copied and pasted. You could edit it out. I don't know what you actually ran.

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.