Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接到websocket时,标题必须同时是字符串和字典?_Python_Web Scraping_Websocket - Fatal编程技术网

Python 连接到websocket时,标题必须同时是字符串和字典?

Python 连接到websocket时,标题必须同时是字符串和字典?,python,web-scraping,websocket,Python,Web Scraping,Websocket,我试图通过自己连接到WebCoket来刮取具有websocket连接的网页。这是我的代码: from websocket import create_connection import json headers = json.dumps({ 'Connection': 'Upgrade', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Upgrade': 'websocket', 'Origin': 'https://www.bet77

我试图通过自己连接到WebCoket来刮取具有websocket连接的网页。这是我的代码:

from websocket import create_connection
import json

headers = json.dumps({
'Connection': 'Upgrade',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Upgrade': 'websocket',
'Origin': 'https://www.bet777.be',
'Sec-WebSocket-Version': 13,
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'es,en;q=0.9,pl;q=0.8,es-AR;q=0.7',
'Sec-WebSocket-Key': 'mYc+hqhy8sUyeilrxyxSPA==',
'Sec-WebSocket-Extensions': 'permessage-deflate; 
client_max_window_bits',
})
ws = create_connection('wss://pushserver- 
uk.sbtech.com/signalr/connecttransport=webSockets&clientProtocol=1.5&connectionToken=nOugyCC54kCePwbLVXCfkfpxZsipI83mU476SdYNspEAD2U0%2F3O44lja67ujJErljZiflHtWyOwELt0OHfQhBQxXu14hVe8zE%2Fr4syolXWBCWWoG%2B2D8WwmUCxi5HSUz4&connectionData=%5B%7B%22name%22%3A%22communicationhub%22%7D%5D&tid=9', header=headers)

ws.send('''{"H":"communicationhub","I":0,"M":"Introduce"}''')
while True:
    print(ws.recv)
我得到了以下错误的答案:

File "/home/gonzalo/.local/lib/python3.6/site-packages/websocket/_handshake.py", line 124, in _get_handshake_headers
key = options['header']['Sec-WebSocket-Key']
TypeError: string indices must be integers
如果我删除Sec WebScoket的关键部分,我会得到400状态响应

知道我做错了什么吗

编辑:使用dict而不是json.dump会导致以下错误。 文件 “/home/gonzalo/.local/lib/python3.6/sitepackages/websocket/_handshake.py”, 第139行,在 如果v不是无


TypeError:sequence item 1:expected str instance,int找到
websocket.create\u connection的
头参数
应该是dict,而不是JSON字符串,因此不要使用
JSON.dumps
将dict转换为字符串,而是直接将dict作为头传递:

headers = {
    'Connection': 'Upgrade',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'Upgrade': 'websocket',
    'Origin': 'https://www.bet777.be',
    'Sec-WebSocket-Version': '13',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'es,en;q=0.9,pl;q=0.8,es-AR;q=0.7',
    'Sec-WebSocket-Key': 'mYc+hqhy8sUyeilrxyxSPA==',
    'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
}
ws = create_connection('wss://pushserver-uk.sbtech.com/signalr/connecttransport=webSockets&clientProtocol=1.5&connectionToken=nOugyCC54kCePwbLVXCfkfpxZsipI83mU476SdYNspEAD2U0%2F3O44lja67ujJErljZiflHtWyOwELt0OHfQhBQxXu14hVe8zE%2Fr4syolXWBCWWoG%2B2D8WwmUCxi5HSUz4&connectionData=%5B%7B%22name%22%3A%22communicationhub%22%7D%5D&tid=9',
    header=headers)

这个错误消息与您的代码有什么关系?我得到了以下错误:File“/home/gonzalo/.local/lib/python3.6/site packages/websocket/_handshake.py”,第139行,if v不是None类型错误:序列项1:预期的str实例,int foundHeader值必须是所有字符串,因此,您应该引用为键
设置的
13
“Sec-WebSocket-Version”
。我已经相应地更新了我的答案。