[Python] OSError 99 while starting socketserver on boot, Debian

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

  1. Stack

    Stack Membro Participativo

    I wrote a simmple TCP server using socketserver module. It runs and functions perfectly well when running it through bash like


    python scpi_server.py
    ​

    Here is the simplified code:

    if __name__ == "__main__":
    HOST, PORT = "10.121.1.36", 9999 # Listen on strict IP given to my by company DHCP on port 9999
    server = socketserver.TCPServer((HOST, PORT), ScpiRequestHandler)
    print(f"Server started on: {server.server_address}")
    server.serve_forever() # Start the server


    The handler looks as such:

    class ScpiRequestHandler(socketserver.BaseRequestHandler):
    def handle(self):
    while True:
    data = self.request.recv(1024).decode('utf-8')
    if not data:
    break
    for cmd in data.split(';'):
    try:
    cmd = ScpiParser.parse(cmd)
    except TypeError:
    #abort if failed
    return

    answer = BusHandler.apply(cmd)

    if answer is not None:
    self.request.sendall(answer.encode('utf-8'))



    As I said the program runs flawlessly if I execute it myself but I tried to make it run on boot by:

    1. Creating scpi_server.service in ~etc/systemd/system~
    2. Giving it permissions by ~chmod +x /path/to/scpi_server.py~
    3. Reloading by ~sudo systemctl daemon-reload~
    4. Enabling the service by ~sudo systemctl enable scpi_server.service~

    The content of scpi_server.service is:

    [Unit]
    Description=My Python Script
    After=network.target
    Wants=network.target

    [Service]
    ExecStart=/usr/bin/python3 /pathto/scpi_server.py
    WorkingDirectory=/pathto/
    User=root #the user i usually loginto
    StandardOutput=journal
    StandardError=journal
    Restart=always

    [Install]
    WantedBy=multi-user.target


    If I call ~systemctl status scpi_server.service~ I can see it running if I manually started it first with ~sudo systemctl enable scpi_server.service~, but not on reboot. If I go into the log files I see the error raised by => server = socketserver.TCPServer((HOST, PORT), ScpiRequestHandler)


    Traceback (most recent call last): File "/root/RaspIO_gpio_interf/scpi_server.py", line 61, in server = socketserver.TCPServer((HOST, PORT), ScpiRequestHandler) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/socketserver.py", line 456, in init self.server_bind() File "/usr/lib/python3.11/socketserver.py", line 472, in server_bind self.socket.bind(self.server_address) OSError: [Errno 99] Cannot assign requested address
    ​

    The application crashes, tries to restart, crashes, tries to restart, crashes....

    Im running the scpi server on a Debian Raspberry 4 that gets its IP address strictly from company DHCP so there is no room to play with that (and the app runs fine if i run it manually...) I am pretty lost on what to do. Please advise. Thank you


    Thank you @JuliusB for the help. I just had to change "After=network-online.target" and "Wants=network-online.target" and now it turns on and functions as expected.

    Continue reading...

Compartilhe esta Página