-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpClient_signals.py
More file actions
43 lines (32 loc) · 876 Bytes
/
tcpClient_signals.py
File metadata and controls
43 lines (32 loc) · 876 Bytes
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
#/bin/python
# Program for messing with tcp clients
import socket
import sys
import signal
TARGET_SERVER = ("127.0.0.1", 8888)
tcpSocket = 0
def signal_handler(signum, frm) :
global tcpSocket
print "SIGINT received"
print "Shutting down client gracefully."
# now shut it down
tcpSocket.close()
print "Exiting now."
sys.exit()
def tcp_client():
global tcpSocket
tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSocket.connect(TARGET_SERVER)
signal.signal(signal.SIGINT, signal_handler)
while 1:
userInput = raw_input("Please enter a string: ")
tcpSocket.send(userInput)
print tcpSocket.recv(1024)
if __name__ == '__main__':
print "Starting tcp client..."
tcp_client()
# SIGINT is the signal for program interupt
signal.signal(signal.SIGINT, signal_handler)
# and now we wait
while True:
pass