在Python中添加Web套接字打开连接请求中的头和cookies

2024-09-27 18:10:47 发布

您现在位置:Python中文网/ 问答频道 /正文

为了对自己进行身份验证,我需要在打开与websocket服务器的连接时添加一个身份验证cookie和一些头字段和值。我已经探索了很多,但没有找到任何简单的、清晰的Python库来帮助我实现这一点。在


Tags: 服务器身份验证cookiewebsocket头字
3条回答

如果它符合您的需要,您可以查看urllib和{}库。它们是更高级别的库,让你可以很容易地完成这类工作。例如,发送带有某些标头字段的请求如下所示:

import urllib2
import urllib

#POST data you might want to send. If you don't want to send post data, just set this as None or blank, e.g. data = {}
data = urllib.urlencode({"username":uname, "passwd":yourpass, "submit":"Submit"})
#add as many header fields as you like
headers = {"Cookie":"<<your cookies go here>>", "Referer":"blahblah"} 

request = urllib2.Request("yourwebserver.com", data, headers)
#here you get your response back
response = urllib2.urlopen(request).read()

希望这对你有所帮助!干杯!:)

您可以使用请求库:文档.python-请求.org在

AutobahnPython支持在WebSocket打开握手期间检查和设置HTTP头。请参见herehere。在

相关问题 更多 >

    热门问题