-1

I have a python script given below which listens to a port. In this case, it is 4995. I need it to be always running and listen for connections and save the received data in a file.

import socket
import threading
from datetime import datetime
import time


class Testing:
    def __init__(self):
        self.PORT = 4995
        self.SERVER = ''
        self.ADDR = (self.SERVER, self.PORT)
        self.count = 0
        

    def handle_client(self, conn, addr):
        print(f'[{datetime.now().strftime("%H:%M")}][NEW CONNECTION] {addr} connected.')
        s = time.time()
        while True:
            if time.time() - s > 300:
                conn.close()
                print(f'[{datetime.now().strftime("%H:%M")}][CLOSE CONNECTION] {addr} closed.')
                break
            try:
                msg = conn.recv(2048).decode('utf-8')
            except ConnectionResetError:
                print(f'[{datetime.now().strftime("%H:%M")}][CLOSE CONNECTION] {addr} closed by peers.')
                break
            else:
                if msg:
                    print(f'[{datetime.now().strftime("%H:%M")}] {msg}')
                    with open('msg.txt','a') as file:
                        file.write(msg)
                    s = time.time()

        # conn.close()

    def start(self):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.bind(self.ADDR)

        self.server.listen()
        print(f'[LISTENING] Server is listening on {self.SERVER}:{self.PORT}')
        while True:
            try:
                self.conn, addr = self.server.accept()
            except OSError:
                return
            thread = threading.Thread(target=self.handle_client, args=(self.conn, addr))
            thread.start()
            print(f'[{datetime.now().strftime("%H:%M")}][ACTIVE CONNECTIONS] {threading.active_count() - 1}')


if __name__ == '__main__':
    Testing().start()


I have tried running the above script like

nohup python3 -u script.py &

And it did work, but after some time, automatically client failed to establish a connection with the server, but the script was running fine. After some research, I found that nohup is not the right way to run my script because it kind of stops the script according to the available resources on Linux machine.

I want my script always to be running and listening for connections.

quei
  • 13
  • 2
  • Does this answer your question? [How to make a Python script run like a service or daemon in Linux](https://stackoverflow.com/questions/1603109/how-to-make-a-python-script-run-like-a-service-or-daemon-in-linux) – esqew Dec 19 '22 at 19:56
  • hi @esqew thanks for ur suggestion but i don't really think that would be right for my kind of script as it is a listener and in ur suggested answer it is a simple script which has to run multiple times and can be done using cron job – Priyansh Khandelwal Dec 19 '22 at 20:03
  • Have a look at: https://stackoverflow.com/a/697064/7216865 – Maurice Meyer Dec 19 '22 at 21:00
  • In my opinion you should set up a listener using something like `socat` and when the port receives a "connection" you specify an action (like running a Python script) .. [Example Here](https://unix.stackexchange.com/questions/314550/how-to-set-a-script-to-execute-when-a-port-receives-a-message) – Zak Dec 19 '22 at 21:04
  • hi @Zak is the solution u provided scalable, like i might be getting 10k tcp connection which will be sending data every 10 secs – Priyansh Khandelwal Dec 19 '22 at 21:22
  • It's as scalable as your hardware will allow it to be without running out of memory, cpu or bandwidth .. – Zak Dec 19 '22 at 21:24

1 Answers1

0

Hi guys finally I found an answer to my query. I ran my code as a service instead of a background process. Steps to follow:-

Step 1:- Create a service with any name u want at the below path. I am creating it with name app.service

sudo vim /etc/systemd/system/app.service

Step 2:- Just copy and paste below code in ur service file. Change 'Description', 'User' and 'ExecStart' as per ur convenience.

[Unit]
Description=my app desc
After=network.target

[Service]
User=root
# command to start the script
ExecStart= python3 /home/user/app.py
Restart=always

[Install]
WantedBy=multi-user.target

Step 3:- sudo systemctl start app

step 4:- sudo systemctl enable app

Don't forget to restart ur service using service app restart, whenever u make changes to ur code.