admin管理员组

文章数量:1400311

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")

本文标签: how recieve data using python script from systemdsocketStack Overflow