0

I never really needed to use socket before and just out of curiosity I tried to write a python script as server and another as client. It worked and there is no issue there. In next step, I tried to use systemd.socket and I stuck there. my client always hangs in receiving data. socat and nc return the data with no issue. I appreciate if someone can tell me what am I doing wrong? Hours of googling and talking to chatgpt did not solve the issue. I checked systemd.socket manual and socket module page but no luck there either.

This is the socket unit:

# test.socket
[Unit] 
Description = test socket 

[Socket] 
ListenStream = 8080 
Accept = yes 

[Install] 
WantedBy = sockets.target

service unit:

# [email protected]
[Unit] 
Description=test service for socket 

[Service] 
ExecStart=/usr/bin/python3 /home/myuser/server.py 
StandardInput=socket
StandardOutput=socket

server:

#!/usr/bin/python3
import sys
sys.stdout.write(sys.stdin.readline())

and this is my client:

#!/usr/bin/python3
import socket

host = '127.0.0.1'
port = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send('Hello')
response = s.recv(1024)
if response:
  print("OK")
2
  • Thanks for pointing out. But it is not relative to the issue. I typed some lines while I was putting them here. Commented Mar 25 at 5:29
  • 2
    You're using .readline, so it is waiting for a newline or end-of-file (socket close). Try s.send('Hello\n'). Commented Mar 25 at 5:53

0

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.