-
Notifications
You must be signed in to change notification settings - Fork 26
/
tcpServer_signals.py
49 lines (39 loc) · 1.1 KB
/
tcpServer_signals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#! /bin/python
# Program for messing with tcp server and signals
import sys, signal, socket, random
# set our socket as a global variable
s = 0
def signal_handler(signum, frm) :
global s
print "SIGINT received"
print "Shutting down listener gracefully."
# now shut it down
s.close()
print "Exiting now."
sys.exit()
def tcp_listner():
global s
# pick a random port and fetch host
port = random.randrange(18000,65535,1)
host = socket.gethostname()
# point SIGINT to signal_handler
signal.signal(signal.SIGINT, signal_handler)
print "Opening port %d." % (port)
try:
# try to create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print 'Socket creation failed!'
sys.exit()
print "Binding to host %s port %d" % (host,port)
s.bind((host,port))
# open up a listener that will accept 1 connection
s.listen(1)
if __name__ == '__main__':
print "Starting tcp listner..."
tcp_listner()
# SIGINT is the signal for program interupt
signal.signal(signal.SIGINT, signal_handler)
# and now we wait
while True:
pass