Python websocket 创建连接
Python websocket create connection
我有这个服务器
我想用这个代码连接到服务器:
ws = create_connection("wss://127.0.0.1:9000")
我需要向 create_connection
添加哪些选项?添加 sslopt={"cert_reqs": ssl.CERT_NONE}
不起作用:
websocket._exceptions.WebSocketBadStatusException: Handshake status 400
这个有效
import asyncio
import websockets
import ssl
async def hello():
async with websockets.connect('wss://127.0.0.1:9000',ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)) as websocket:
data = 'hi'
await websocket.send(data)
print("> {}".format(data))
response = await websocket.recv()
print("< {}".format(response))
asyncio.get_event_loop().run_until_complete(hello())
对我来说,问题中的选项似乎有效:
from websocket import create_connection
import ssl
ws = create_connection("wss://echo.websocket.org", sslopt={"cert_reqs": ssl.CERT_NONE})
ws.send("python hello!")
print (ws.recv())
ws.close()
另见此处:
https://github.com/websocket-client/websocket-client#faq
注意:我使用 win7 和 python 3.6.5 并安装了以下软件包 (pip):
- simple-websocket-server==0.4.0
- websocket-客户端==0.53.0
- websockets==6.0
我有这个服务器
我想用这个代码连接到服务器:
ws = create_connection("wss://127.0.0.1:9000")
我需要向 create_connection
添加哪些选项?添加 sslopt={"cert_reqs": ssl.CERT_NONE}
不起作用:
websocket._exceptions.WebSocketBadStatusException: Handshake status 400
这个有效
import asyncio
import websockets
import ssl
async def hello():
async with websockets.connect('wss://127.0.0.1:9000',ssl=ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)) as websocket:
data = 'hi'
await websocket.send(data)
print("> {}".format(data))
response = await websocket.recv()
print("< {}".format(response))
asyncio.get_event_loop().run_until_complete(hello())
对我来说,问题中的选项似乎有效:
from websocket import create_connection
import ssl
ws = create_connection("wss://echo.websocket.org", sslopt={"cert_reqs": ssl.CERT_NONE})
ws.send("python hello!")
print (ws.recv())
ws.close()
另见此处: https://github.com/websocket-client/websocket-client#faq
注意:我使用 win7 和 python 3.6.5 并安装了以下软件包 (pip):
- simple-websocket-server==0.4.0
- websocket-客户端==0.53.0
- websockets==6.0