有一种反爬手段“检测headers请求头中字段的排序”,websocket方式发请求也不例外。
python实现websocket连接,可以使用py包websocket-client
安装
pip install websocket-client
使用
import websocket
def on_message(ws, message):
print(ws)
print(message)
def on_error(ws, error):
print(ws)
print(error)
def on_close(ws):
print(ws)
print("### closed ###")
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://127.0.0.1:8888/track",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
请求测试默认会添加请求头
如何修改并固定排序规则,重写get_handshake_headers函数
from websocket import _handshake
def get_handshake_headers(resource, url, host, port, options):
headers = ['GET /api/challenge64 HTTP/1.1', 'Host: www.python-spider.com', 'Connection: Upgrade',
'Pragma: no-cache', 'Cache-Control: no-cache',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/113.0.0.0 Safari/537.36',
'Upgrade: websocket', 'Origin: https://www.python-spider.com', 'Sec-WebSocket-Version: 13',
'Accept-Encoding: gzip, deflate, br', 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
'Cookie: sessionid=sm85gembpmofo5mhzustgiwgcwkgk5dm;', 'Sec-WebSocket-Key: y1H/4iwgf/4st9XYL0j+mg==',
'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits', '', '']
key = 'y1H/4iwgf/4st9XYL0j+mg=='
return headers, key
_handshake._get_handshake_headers = get_handshake_headers
测试
文章来源:https://www.toymoban.com/news/detail-654707.html
大功告成! 文章来源地址https://www.toymoban.com/news/detail-654707.html
到了这里,关于python自定义websocket包headers请求头的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!