Python 使用Tornado发送带有WebSocket的cookie

Python 使用Tornado发送带有WebSocket的cookie,python,tornado,Python,Tornado,我目前正在使用tornado连接websocket,有没有一种方法可以通过websocket连接传递cookie import logging import tornado.httpclient import tornado.gen import tornado.options import tornado.web import tornado.websocket @tornado.gen.coroutine def connect_websocket(): url = torna

我目前正在使用tornado连接websocket,有没有一种方法可以通过websocket连接传递cookie

import logging

import tornado.httpclient
import tornado.gen
import tornado.options
import tornado.web
import tornado.websocket

@tornado.gen.coroutine
def connect_websocket():

    url = tornado.options.options.ws_host

    try:
        ws_connection = yield tornado.websocket.websocket_connect(url, connect_timeout=5)
        logging.info("Connection established (%s), waiting for output...", url)
    except Exception as conn_err:
        logging.error("Error connecting to %s", conn_err)
        return

    while True:
        output = yield ws_connection.read_message()
        logging.info(output)


if __name__ == '__main__':
    tornado.options.define(name="ws_host", type=str, help="Websocket host address.")
    tornado.options.parse_command_line()

    tornado.ioloop.IOLoop.instance().run_sync(connect_websocket)

谢谢

websocket\u connect(url)中的
url
参数可以是普通的url字符串,也可以是对象。虽然没有文档记录,但您可以在中看到这一点

因此,您可以创建一个
HTTPRequest
的实例并在那里设置Cookie头,毕竟Cookie只是一个头

例如:

from tornado import httpclient

# create an instace of HTTPRequest with the given url
request = httpclient.HTTPRequest(url, headers={'Cookie': 'name=value'})

# connect to ws using the request object
ws_connection = yield tornado.websocket.websocket_connect(request)