1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] how recieve data using python script from systemd.socket

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    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:

    # test@.service
    [Unit]
    Description=test service for socket

    [Service]
    ExecStart=/user/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")

    Continue reading...

Compartilhe esta Página