python实现的websocket代码Word文件下载.docx
《python实现的websocket代码Word文件下载.docx》由会员分享,可在线阅读,更多相关《python实现的websocket代码Word文件下载.docx(13页珍藏版)》请在冰豆网上搜索。
viewplain
copy
1.#coding=utf8
2.#!
/usr/bin/python
3.
4.
5.import
struct,socket
6.import
hashlib
7.import
threading,random
8.import
time
9.import
struct
10.from
base64
import
b64encode,
b64decode
11.
12.
13.connectionlist
=
{}
14.g_code_length
0
15.g_header_length
16.
17.
18.def
hex2dec(string_num):
19.
return
str(int(string_num.upper(),
16))
20.
21.
22.
23.
24.def
get_datalength(msg):
25.
global
g_code_length
26.
g_header_length
27.
28.
print
(len(msg))
29.
ord(msg[1])
&
127
30.
received_length
0;
31.
if
==
126:
32.
#g_code_length
msg[2:
4]
33.
(ord(msg[2])<
<
8)
+
(ord(msg[3]))
34.
struct.unpack('
>
H'
str(msg[2:
4]))[0]
35.
8
36.
elif
127:
37.
10]
38.
Q'
10]))[0]
39.
14
40.
else:
41.
6
42.
int(g_code_length)
43.
44.
45.def
parse_data(msg):
46.
47.
48.
49.
50.
51.
masks
msg[4:
8]
52.
data
msg[8:
]
53.
54.
55.
msg[10:
14]
56.
msg[14:
57.
58.
6]
59.
msg[6:
60.
61.
62.
i
63.
raw_str
'
64.
65.
66.
for
d
in
data:
67.
+=
chr(ord(d)
^
ord(masks[i%4]))
68.
1
69.
70.
71.
(u"
总长度是:
%d"
%
int(g_code_length))
72.
73.
74.
75.def
sendMessage(message):
76.
connectionlist
77.
78.
message_utf_8
message.encode('
utf-8'
)
79.
connection
connectionlist.values():
80.
back_str
[]
81.
back_str.append('
\x81'
82.
data_length
len(message_utf_8)
83.
84.
85.
125:
86.
back_str.append(chr(data_length))
87.
65535
:
88.
back_str.append(struct.pack('
b'
126))
89.
h'
data_length))
90.
#back_str.append(chr(data_length
8))
91.
0xFF))
92.
#a
struct.pack('
data_length)
93.
#b
chr(data_length
94.
#c
0xFF)
95.
(2^64-1):
96.
#back_str.append(chr(127))
97.
127))
98.
q'
99.
100.
101.
else
102.
(u'
太长了'
103.
msg
104.
c
back_str:
105.
c;
106.
str(msg)
message_utf_8#.encode('
107.
#connection.send(str.encode(str(u"
\x00%s\xFF\n\n"
message)))
#这个是旧版
108.
#print
send
message:
message)
109.
!
None
and
len(back_str)
0:
110.
(back_str)
111.
connection.send(back_str)
112.
113.
114.def
deleteconnection(item):
115.
116.
del
connectionlist['
connection'
+item]
117.
118.
119.class
WebSocket(threading.Thread):
#继承Thread
120.
121.
122.
GUID
"
258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
123.
124.
125.
def
__init__(self,conn,index,name,remote,
path="
/"
):
126.
threading.Thread.__init__(self)#初始化父类Thread
127.
self.conn
conn
128.
self.index
index
129.
self.name
name
130.
self.remote
remote
131.
self.path
path
132.
self.buffer
133.
self.buffer_utf8
134.
self.length_buffer
135.
run(self):
#重载Thread的run
136.
print('
Socket%s
Start!
self.index)
137.
headers
138.
self.handshaken
False
139.
140.
141.
while
True:
142.
False:
143.
('
Start
Handshaken
with
%s!
(self.index,self.remote))
144.
bytes.decode(self.conn.recv(1024))
145.
146.
147.
self.buffer.find('
\r\n\r\n'
-1:
148.
header,
self.buffer.split('
1)
149.
line
header.split("
\r\n"
)[1:
]:
150.
key,
value
line.split("
151.
headers[key]
152.
153.
154.
headers["
Location"
("
ws:
//%s%s"
%(headers["
Host"
],
self.path))
155.
key
headers['
Sec-WebSocket-Key'
156.
token
b64encode(hashlib.sha1(str.encode(str(key
self.GUID))).digest())
157.
158.
159.
handshake="
HTTP/1.1
101
Switching
Protocols\r\n"
\
160.
Upgrade:
websocket\r\n"
161.
Connection:
Upgrade\r\n"
162.
Sec-WebSocket-Accept:
+bytes.decode(token)+"
163.
WebSocket-Origin:
+str(headers["
Origin"
])+"
164.
WebSocket-Location:
\r\n\r\n"
165.
166.
167.
self.conn.send(str.encode(str(handshake)))
168.
True
169.
Socket
%s
success!
%(self.index,
self.remote))
170.
sendMessage(u'
Welcome,
171.
172.
173.
174.
175.
176.
177.
178.
mm=self.conn.recv(128)
179.
len(mm)
180.
continue
181.
182.
get_datalength(mm)
183.
#接受的长度
184.
185.
mm
186.
-
187.
188.
189.
parse_data(self.buffer)
#utf8
190.
msg_unicode
str(self.buffer_utf8).decode('
ignore'
#unicode
191.
msg_unicode=='
quit'
192.
Logout!
(self.index))
193.
nowTime
time.strftime('
%H:
%M:
%S'
time.localtime(time.time()))
194.
sa