4

I have the below python script(server.py) in order to listen to a port and capture the requests from the client. I am calling this script from another python file(Main.py). My requirement is to stop executing server.py after certain amount of time. I tried using exit() at the last line of the file - server.py to stop the server and stop the execution of the file, however I was not able to stop the script from running and the server kept responding. Can anyone help me in letting me know where I am going wrong.

Server.py

bind_ip = "127.0.0.1"
bind_port = 2530

def servercreate():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((bind_ip,bind_port))
    server.listen(5)

    while True:
        client, addr = server.accept()
        print('[*] Accepted connection from: %s:%d' % (addr[0], addr[1]))
        client_handler = threading.Thread(target=handle_client, args=(client,))
        client_handler.start()


def handle_client(client_socket):
    request = client_socket.recv(2056)
    print('[*] Received: %s' % request)

    message = "{}"
    client_socket.send(message.encode('utf-8'))
    client_socket.close()

if __name__ == '__main__':
    servercreate()

Main.py

import Server
Server.servercreate()
2
  • Well, the quick and dirty way is to set client_handler.daemon = True before client_handler.start(). The less forceful method would be to have a terminate flag and check that periodically in the client threads. Commented Apr 9, 2017 at 18:07
  • Hi @dhke, thanks for the comment. Can you please be a bot more specific as I am new to Python programming. Thanks. Commented Apr 9, 2017 at 18:13

1 Answer 1

3

if you don't want your code interrupted by time.sleep (which I think stops the code from running), use this:

import time
timeout = time.time() + 10

while True:
    print ('hello')
    if time.time() > timeout:
        print ('program terminated')
        break

if you want 10 minutes worth of time use:

timeout = time.time() + 60*10   

If you just want to stop the program running after a certain amount of time use something like

import time

x=0
while True:
    print ('waiting 5')
    time.sleep(5)
    x += 1
    if x == (10):
        break

the time.sleep is in seconds, break stops the loop and should end your program

update, try this:

import time


bind_ip = "127.0.0.1"
bind_port = 2530

def servercreate():

    #put minutes of time you want program to run for below
    minutes = 10
    timeout = time.time() + (60*minutes)

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((bind_ip,bind_port))
    server.listen(5)

    while True:
        client, addr = server.accept()
        print('[*] Accepted connection from: %s:%d' % (addr[0], addr[1]))
        client_handler = threading.Thread(target=handle_client, args=(client,))
        client_handler.start()
        if time.time() > timeout:
            break


def handle_client(client_socket):
    request = client_socket.recv(2056)
    print('[*] Received: %s' % request)

    message = "{}"
    client_socket.send(message.encode('utf-8'))
    client_socket.close()


if __name__ == '__main__':
    servercreate()
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @new_to_coding, Thanks for the response. I used the first option in my code in the following way. But it did not work. if __name__ == '__main__': x = 0 while True: servercreate() time.sleep(5) x += 1 print(x) if x == 10: break
@Viman I have made an update with a suggestion, try it out and see if it works, change the minutes at the top if you want your program to run for more or less than 10 minutes
@Viman sorry I saw that I put the time out variable inside the loop, was perhaps meant to be outside of it to avoid continuously setting the timeout variable, so I have edited my answer just in case you need it
Yeah, I was able to get that yesterday @new_to_coding . But the main logic you gave worked fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.