-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_server.py
87 lines (58 loc) · 1.54 KB
/
python_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from socket import *
import time
import StringIO
from PIL import Image
import base64
def decode_base64(data):
"""Decode base64, padding being optional.
:param data: Base64 data as an ASCII byte string
:returns: The decoded byte string.
"""
missing_padding = 4 - len(data) % 4
if missing_padding:
data += b'='* missing_padding
return base64.b64decode(data)
host = "0.0.0.0"
serverSocket=socket(AF_INET,SOCK_STREAM)
serverSocket.bind((host,10000))
totalLength=""
serverSocket.listen(10000)
print "Ready to serve"
data = []
message = ""
##connectionSocket,clientAddress=serverSocket.accept()
##message =connectionSocket.recv(4096)
##connectionSocket.close()
##length = message
##print length
connectionSocket,clientAddress=serverSocket.accept()
print clientAddress[1]
while 1:
try:
message =connectionSocket.recv(16384)
data.append(message)
if len(message) == 0:
print "Ended transmission"
break
## print "No breaks"
## print len(message)
except:
print "Breaks"
break
print "Transmitted"
connectionSocket.send("BLAH")
connectionSocket.close()
serverSocket.close()
s = socket(AF_INET,SOCK_STREAM)
s.connect((clientAddress[0],50000))
s.send("frog")
s.close()
##print len(data)
fh = open("imageToSave.jpg", "wb")
##dS = decode_base64(data)
##fh.write(dS)
##fh.close()
for x in range(len(data)):
fh.write(data[x])
fh.close()
print "Done"