-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPytcp_server.py
More file actions
33 lines (23 loc) · 982 Bytes
/
Pytcp_server.py
File metadata and controls
33 lines (23 loc) · 982 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
# python based script which creates a basic tcp_ server listening to clients for specidied port and with supplied ip address
# 789sk.gupta@gmail.com
import socket
import threading
bind_ip = '0.0.0.0'
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5) # max backlog of connections
print 'Listening on {}:{}'.format(bind_ip, bind_port)
def handle_client_connection(client_socket):
request = client_socket.recv(1024)
print 'Received {}'.format(request)
client_socket.send('ACK!')
while True:
client_sock, address = server.accept()
print 'Accepted connection from {}:{}'.format(address[0], address[1])
client_handler = threading.Thread(
target=handle_client_connection,
args=(client_sock,) # without comma you'd get a... TypeError: handle_client_connection() argument after * must be a sequence, not _socketobject
)
client_handler.start()
# ends