-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_server.py
46 lines (33 loc) · 878 Bytes
/
simple_server.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
import socket
import sys
import thread
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
def clientThread(conn, image_num):
received = ""
while True:
data = conn.recv(1024)
if not data:
with open('test-image-upload-' + str(image_num) + '.jpg', 'w+') as f:
f.write(received)
break
received += data
print 'Received data!'
conn.close()
i = 0
while True:
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
thread.start_new_thread(clientThread, (conn, i))
i += 1
s.close()