當前位置:首頁 » 編程語言 » python實現websocket

python實現websocket

發布時間: 2022-07-16 19:18:21

① 求用python通過websocket協議寫的一個伺服器

defhandshake(conn):
key=None
data=conn.recv(8192)
ifnotlen(data):
returnFalse
forlineindata.split(' ')[0].split(' ')[1:]:
k,v=line.split(':')
ifk=='Sec-WebSocket-Key':
key=base64.b64encode(hashlib.sha1(v+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest())
ifnotkey:
conn.close()
returnFalse
response='HTTP/1.1101SwitchingProtocols '
'Upgrade:websocket '
'Connection:Upgrade '
'Sec-WebSocket-Accept:'+key+' '
conn.send(response)
returnTrue

② python 怎麼解析 websocket 傳來的漢字 亂碼

使用socket的時候,如果你傳送的是str類型的漢字,就不會出現亂碼

③ 用python實現的websocket server哪個是高性能的

推薦 tornado,非同步並發。和 node.js 的實現有得拼。

④ 如何用python獲取websocket數據

這里,介紹如何使用 Python 與前端 js 進行通信。
websocket 使用 HTTP 協議完成握手之後,不通過 HTTP 直接進行 websocket 通信。
於是,使用 websocket 大致兩個步驟:使用 HTTP 握手,通信。
js 處理 websocket 要使用 ws 模塊; python 處理則使用 socket 模塊建立 TCP 連接即可,比一般的 socket ,只多一個握手以及數據處理的步驟。
握手
過程

包格式
js 客戶端先向伺服器端 python 發送握手包,格式如下:
?

1
2
3
4
5
6
7
8

GET
/chat HTTP/1.1
Host:
server.example.com
Upgrade:
websocket
Connection:
Upgrade
Sec-WebSocket-Key:
dGhlIHNhbXBsZSBub25jZQ==
Origin:
Sec-WebSocket-Protocol:
chat, superchat
Sec-WebSocket-Version:
13

伺服器回應包格式:
?

1
2
3
4
5

HTTP/1.1
101 Switching Protocols
Upgrade:
websocket
Connection:
Upgrade
Sec-WebSocket-Accept:
s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol:
chat

其中, Sec-WebSocket-Key 是隨機的,伺服器用這些數據構造一個 SHA-1 信息摘要。
方法為: key+migic , SHA-1 加密, base-64 加密,如下:

Python 中的處理代碼
?

1
2

MAGIC_STRING='258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
res_key=base64.b64encode(hashlib.sha1(sec_key
+MAGIC_STRING).digest())

握手完整代碼
js 端
js 中有處理 websocket 的類,初始化後自動發送握手包,如下:
var socket = new WebSocket('ws://localhost:3368');
Python 端
Python 用 socket 接受得到握手字元串,處理後發送
?

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

HOST='localhost'
PORT=3368
MAGIC_STRING='258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
HANDSHAKE_STRING="HTTP/1.1
101 Switching Protocols\r\n"
\
"Upgrade:websocket\r\n"\
"Connection:
Upgrade\r\n"
\
"Sec-WebSocket-Accept:
{1}\r\n"
\
"WebSocket-Location:ws://{2}/chat\r\n"

\
"WebSocket-Protocol:chat\r\n\r\n"

defhandshake(con):
#con為用socket,accept()得到的socket

headers={}
shake=con.recv(1024)

ifnot

len(shake):
returnFalse

header,
data =shake.split('\r\n\r\n',1)
forline
inheader.split('\r\n')[1:]:
key,
val =line.split(':
',1)
headers[key]=val

if'Sec-WebSocket-Key'

not
in
headers:
print('This
socket is not websocket, client close.')
con.close()
returnFalse

sec_key=headers['Sec-WebSocket-Key']
res_key=base64.b64encode(hashlib.sha1(sec_key
+MAGIC_STRING).digest())

str_handshake=HANDSHAKE_STRING.replace('{1}',
res_key).replace('{2}',
HOST +':'

+
str(PORT))
printstr_handshake
con.send(str_handshake)
returnTrue

通信
不同版本的瀏覽器定義的數據幀格式不同, Python 發送和接收時都要處理得到符合格式的數據包,才能通信。
Python 接收
Python 接收到瀏覽器發來的數據,要解析後才能得到其中的有用數據。
瀏覽器包格式

固定位元組:
( 1000 0001 或是 1000 0002 )這里沒用,忽略
包長度位元組:
第一位肯定是 1 ,忽略。剩下 7 個位可以得到一個整數 (0 ~ 127) ,其中
( 1-125 )表此位元組為長度位元組,大小即為長度;
(126)表接下來的兩個位元組才是長度;
(127)表接下來的八個位元組才是長度;
用這種變長的方式表示數據長度,節省數據位。
mark 掩碼:
mark 掩碼為包長之後的 4 個位元組,之後的兄弟數據要與 mark 掩碼做運算才能得到真實的數據。
兄弟數據:
得到真實數據的方法:將兄弟數據的每一位 x ,和掩碼的第 i%4 位做 xor 運算,其中 i 是 x 在兄弟數據中的索引。
完整代碼
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

defrecv_data(self,
num):
try:
all_data=self.con.recv(num)
ifnot

len(all_data):
returnFalse
except:
returnFalse
else:
code_len=ord(all_data[1])
& 127
ifcode_len
==126:
masks=all_data[4:8]
data=all_data[8:]
elifcode_len
==127:
masks=all_data[10:14]
data=all_data[14:]
else:
masks=all_data[2:6]
data=all_data[6:]
raw_str=""
i=0
ford
indata:
raw_str+=chr(ord(d)
^ ord(masks[i%4]))
i+=1
returnraw_str

js 端的 ws 對象,通過 ws.send(str) 即可發送
ws.send(str)
Python 發送
Python 要包數據發送,也需要處理,發送包格式如下

固定位元組:固定的 1000 0001( 『 \x81 ′ )
包長:根據發送數據長度是否超過 125 , 0xFFFF(65535) 來生成 1 個或 3 個或 9 個位元組,來代表數據長度。
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

defsend_data(self,
data):
ifdata:
data=str(data)
else:
returnFalse
token="\x81"
length=len(data)
iflength
< 126:
token+=struct.pack("B",
length)
eliflength
<=0xFFFF:
token+=struct.pack("!BH",126,
length)
else:
token+=struct.pack("!BQ",127,
length)
#struct為Python中處理二進制數的模塊,二進制流為C,或網路流的形式。
data='%s%s'

%
(token, data)
self.con.send(data)
returnTrue

js 端通過回調函數 ws.onmessage() 接受數據
?

1
2
3
4
5

ws.onmessage
= function(result,nTime){
alert("從服務端收到的數據:");
alert("最近一次發送數據到現在接收一共使用時間:"+
nTime);
console.log(result);
}

最終代碼
Python服務端
?

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

#
_*_ coding:utf-8 _*_
__author__='Patrick'

importsocket
importthreading
importsys
importos
importMySQLdb
importbase64
importhashlib
importstruct

#
====== config ======
HOST='localhost'
PORT=3368
MAGIC_STRING='258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
HANDSHAKE_STRING="HTTP/1.1
101 Switching Protocols\r\n"
\
"Upgrade:websocket\r\n"\
"Connection:
Upgrade\r\n"
\
"Sec-WebSocket-Accept:
{1}\r\n"
\
"WebSocket-Location:ws://{2}/chat\r\n"

\
"WebSocket-Protocol:chat\r\n\r\n"

classTh(threading.Thread):
def__init__(self,
connection,):
threading.Thread.__init__(self)
self.con=connection

defrun(self):
whileTrue:
try:
pass
self.con.close()

defrecv_data(self,
num):
try:
all_data=self.con.recv(num)
ifnot

len(all_data):
returnFalse
except:
returnFalse
else:
code_len=ord(all_data[1])
& 127
ifcode_len
==126:
masks=all_data[4:8]
data=all_data[8:]
elifcode_len
==127:
masks=all_data[10:14]
data=all_data[14:]
else:
masks=all_data[2:6]
data=all_data[6:]
raw_str=""
i=0
ford
indata:
raw_str+=chr(ord(d)
^ ord(masks[i%4]))
i+=1
returnraw_str

#
send data
defsend_data(self,
data):
ifdata:
data=str(data)
else:
returnFalse
token="\x81"
length=len(data)
iflength
< 126:
token+=struct.pack("B",
length)
eliflength
<=0xFFFF:
token+=struct.pack("!BH",126,
length)
else:
token+=struct.pack("!BQ",127,
length)
#struct為Python中處理二進制數的模塊,二進制流為C,或網路流的形式。
data='%s%s'

%
(token, data)
self.con.send(data)
returnTrue

#
handshake
defhandshake(con):
headers={}
shake=con.recv(1024)

ifnot

len(shake):
returnFalse

header,
data =shake.split('\r\n\r\n',1)
forline
inheader.split('\r\n')[1:]:
key,
val =line.split(':
',1)
headers[key]=val

if'Sec-WebSocket-Key'

not
in
headers:
print('This
socket is not websocket, client close.')
con.close()
returnFalse

sec_key=headers['Sec-WebSocket-Key']
res_key=base64.b64encode(hashlib.sha1(sec_key
+MAGIC_STRING).digest())

str_handshake=HANDSHAKE_STRING.replace('{1}',
res_key).replace('{2}',
HOST +':'

+
str(PORT))
printstr_handshake
con.send(str_handshake)
returnTrue

defnew_service():
"""start
a service socket and listen
when
coms a connection, start a new thread to handle it"""

sock=socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
try:
sock.bind(('localhost',3368))
sock.listen(1000)
#鏈接隊列大小
print"bind
3368,ready to use"
except:
print("Server
is already running,quit")
sys.exit()

whileTrue:
connection,
address =sock.accept()
#返回元組(socket,add),accept調用時會進入waite狀態
print"Got
connection from ",
address
ifhandshake(connection):
print"handshake
success"
try:
t=Th(connection,
layout)
t.start()
print'new
thread for client ...'
except:
print'start
new thread error'
connection.close()

if__name__
=='__main__':
new_service()

js客戶 端
?

1
2
3
4
5
6
7
8

<script>
varsocket
= newWebSocket('ws://localhost:3368');
ws.onmessage
= function(result,nTime){
alert("從服務端收到的數據:");
alert("最近一次發送數據到現在接收一共使用時間:"+
nTime);
console.log(result);
}
</script>

⑤ 關於Python版本的websocket連接器

如果只是想實驗多client的場景,一台電腦用不同的瀏覽器就可以充當不同的client了。

⑥ 想用Python寫一個在線聊天系統,有什麼模塊能用來處理WebSocket嗎

伺服器:
import socket
s=socket.socket()
s.bind(('xxx.xxx.xxx.xxx',xxxx)) #ip地址和埠號
s.listen(5)
cs,address = s.accept()
print 'got connected from',address
cs.send('bye')
ra=cs.recv(512)
print ra
cs.close()

客戶端:
import socket
s=socket.socket()
s.connect(('xxx.xxx.xxx.xxx',xxxx)) #與伺服器程序ip地址和埠號相同
data=s.recv(512)
s.send('hihi')
s.close()
print 'the data received is',data

然後反復調整、嘗試。

⑦ python怎麼連接websocket

websocket是一個瀏覽器和伺服器通信的新的協議,一般而言,瀏覽器和伺服器通信最常用的是http協議,但是http協議是無狀態的,每次瀏覽器請求信息,伺服器返回信息後這個瀏覽器和伺服器通信的信道就被關閉了,這樣使得伺服器如果想主動給瀏覽器發送信息變得不可能了,伺服器推技術在http時代的解決方案一個是客戶端去輪詢,或是使用comet技術,而websocket則和一般的socket一樣,使得瀏覽器和伺服器建立了一個雙工的通道。
具體的websocket協議在rfc6455裡面有,這里簡要說明一下。websocket通信需要先有個握手的過程,使得協議由http轉變為webscoket協議,然後瀏覽器和伺服器就可以利用這個socket來通信了。

首先瀏覽器發送握手信息,要求協議轉變為websocket

GET / HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin:
伺服器接收到信息後,取得其中的Sec-WebSocket-Key,將他和一個固定的字元串258EAFA5-E914-47DA-95CA-C5AB0DC85B11做拼接,得到的字元串先用sha1做一下轉換,再用base64轉換一下,就得到了回應的字元串,這樣伺服器端發送回的消息是這樣的

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
這樣握手就完成了,用python來實現這段握手過程的話就是下面這樣。

def handshake(conn):
key =None
data = conn.recv(8192)
if not len(data):
return False
for line in data.split('\r\n\r\n')[0].split('\r\n')[1:]:
k, v = line.split(': ')
if k =='Sec-WebSocket-Key':
key =base64.b64encode(hashlib.sha1(v +'258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest())
if not key:
conn.close()
return False
response ='HTTP/1.1 101 Switching Protocols\r\n'\
'Upgrade: websocket\r\n'\
'Connection: Upgrade\r\n'\
'Sec-WebSocket-Accept:'+ key +'\r\n\r\n'
conn.send(response)
return True
握手過程完成之後就是信息傳輸了,websocket的數據信息格式是這樣的。

+-+-+-+-+-------+-+-------------+-------------------------------+
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| | Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
值得注意的是payload len這項,表示數據的長度有多少,如果小於126,那麼payload len就是數據的長度,如果是126那麼接下來2個位元組是數據長度,如果是127表示接下來8個位元組是數據長度,然後後面會有四個位元組的mask,真實數據要由payload data和mask做異或才能得到,這樣就可以得到數據了。發送數據的格式和接受的數據類似,具體細節可以去參考rfc6455,這里就不過多贅述了。

Python的Websocket客戶端:Websocket-Client

Websocket-Client 是 Python 上的 Websocket 客戶端。它只支持 hybi-13,且所有的 Websocket API 都支持同步。

Installation
This mole is tested on Python 2.7 and Python 3.x.
Type "python setup.py install" or "pip install websocket-client" to install.
Caution!
from v0.16.0, we can install by "pip install websocket-client" for python 3.
This mole depend on
six
backports.ssl_match_hostname for Python 2.x

Python通過websocket與js客戶端通信示例分析

這里,介紹如何使用 Python 與前端 js 進行通信。
websocket 使用 HTTP 協議完成握手之後,不通過 HTTP 直接進行 websocket 通信。
於是,使用 websocket 大致兩個步驟:使用 HTTP 握手,通信。
js 處理 websocket 要使用 ws 模塊; Python 處理則使用 socket 模塊建立 TCP 連接即可,比一般的 socket ,只多一個握手以及數據處理的步驟。

包格式
js 客戶端先向伺服器端 python 發送握手包,格式如下:
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin:
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

伺服器回應包格式:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: chat

其中, Sec-WebSocket-Key 是隨機的,伺服器用這些數據構造一個 SHA-1 信息摘要。
方法為: key+migic , SHA-1 加密, base-64 加密

Python 中的處理代碼:
MAGIC_STRING = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
res_key = base64.b64encode(hashlib.sha1(sec_key + MAGIC_STRING).digest())

握手完整代碼
js 端
js 中有處理 websocket 的類,初始化後自動發送握手包,如下:
var socket = new WebSocket('ws://localhost:3368');
Python 端
Python 用 socket 接受得到握手字元串,處理後發送
HOST = 'localhost'
PORT = 3368
MAGIC_STRING = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
HANDSHAKE_STRING = "HTTP/1.1 101 Switching Protocols\r\n" \
"Upgrade:websocket\r\n" \
"Connection: Upgrade\r\n" \
"Sec-WebSocket-Accept: {1}\r\n" \
"WebSocket-Location: ws://{2}/chat\r\n" \
"WebSocket-Protocol:chat\r\n\r\n"

def handshake(con):
#con為用socket,accept()得到的socket

headers = {}
shake = con.recv(1024)

if not len(shake):
return False

header, data = shake.split('\r\n\r\n', 1)
for line in header.split('\r\n')[1:]:
key, val = line.split(': ', 1)
headers[key] = val

if 'Sec-WebSocket-Key' not in headers:
print ('This socket is not websocket, client close.')
con.close()
return False

sec_key = headers['Sec-WebSocket-Key']
res_key = base64.b64encode(hashlib.sha1(sec_key + MAGIC_STRING).digest())

str_handshake = HANDSHAKE_STRING.replace('{1}', res_key).replace('{2}', HOST + ':' + str(PORT))
print str_handshake
con.send(str_handshake)
return True

通信
不同版本的瀏覽器定義的數據幀格式不同, Python 發送和接收時都要處理得到符合格式的數據包,才能通信。
Python 接收
Python 接收到瀏覽器發來的數據,要解析後才能得到其中的有用數據。

固定位元組:
( 1000 0001 或是 1000 0002 )這里沒用,忽略
包長度位元組:
第一位肯定是 1 ,忽略。剩下 7 個位可以得到一個整數 (0 ~ 127) ,其中
( 1-125 )表此位元組為長度位元組,大小即為長度;
(126)表接下來的兩個位元組才是長度;
(127)表接下來的八個位元組才是長度;
用這種變長的方式表示數據長度,節省數據位。
mark 掩碼:
mark 掩碼為包長之後的 4 個位元組,之後的兄弟數據要與 mark 掩碼做運算才能得到真實的數據。
兄弟數據:
得到真實數據的方法:將兄弟數據的每一位 x ,和掩碼的第 i%4 位做 xor 運算,其中 i 是 x 在兄弟數據中的索引。

完整代碼
def recv_data(self, num):
try:
all_data = self.con.recv(num)
if not len(all_data):
return False
except:
return False
else:
code_len = ord(all_data[1]) & 127
if code_len == 126:
masks = all_data[4:8]
data = all_data[8:]
elif code_len == 127:
masks = all_data[10:14]
data = all_data[14:]
else:
masks = all_data[2:6]
data = all_data[6:]
raw_str = ""
i = 0
for d in data:
raw_str += chr(ord(d) ^ ord(masks[i % 4]))
i += 1
return raw_str
js 端的 ws 對象,通過 ws.send(str) 即可發送
ws.send(str)

Python 發送
Python 要包數據發送,也需要處理
固定位元組:固定的 1000 0001( 『 \x81 ′ )
包長:根據發送數據長度是否超過 125 , 0xFFFF(65535) 來生成 1 個或 3 個或 9 個位元組,來代表數據長度。
def send_data(self, data):
if data:
data = str(data)
else:
return False
token = "\x81"
length = len(data)
if length < 126:
token += struct.pack("B", length)
elif length <= 0xFFFF:
token += struct.pack("!BH", 126, length)
else:
token += struct.pack("!BQ", 127, length)
#struct為Python中處理二進制數的模塊,二進制流為C,或網路流的形式。
data = '%s%s' % (token, data)
self.con.send(data)
return True

⑧ 請問各位大大,Python如何編寫websocket的服務端和客戶端,wss的那種

安裝dwebsocket(pip install dwebsocket ) 後參考下面的鏈接

我django 2.0 測試通過

網頁鏈接

⑨ 如何使用python製作websocket伺服器

from SocketServer import ThreadingTCPServer, StreamRequestHandler

class MyStreamRequestHandler(StreamRequestHandler):
def handle(self):
while True:

# 邏輯

pass

if __name__ == "__main__":
host = ""
port = 12345

addr = (host, port)

server = ThreadingTCPServer(addr, MyStreamRequestHandler)

server.serve_forever()

在supervisor中執行這個腳本就可以了。

熱點內容
愛奇藝會員怎麼改密碼 發布:2025-03-06 11:33:44 瀏覽:58
firefox不緩存 發布:2025-03-06 11:33:43 瀏覽:464
淘寶密碼如何破解 發布:2025-03-06 11:32:56 瀏覽:591
sqlservereclipse 發布:2025-03-06 11:25:29 瀏覽:704
linux存放文件 發布:2025-03-06 11:24:47 瀏覽:444
nfslinux掛載 發布:2025-03-06 11:19:42 瀏覽:233
安卓動態壁紙怎麼提取 發布:2025-03-06 11:07:26 瀏覽:111
有鎖安卓手機有什麼壞處 發布:2025-03-06 11:00:20 瀏覽:575
dvwa上傳 發布:2025-03-06 10:46:58 瀏覽:699
新款榮放低配有哪些新配置 發布:2025-03-06 10:41:29 瀏覽:791