I have this server
https://github.com/crossbario/autobahn-python/blob/master/examples/twisted/websocket/echo_tls/server.py
And I want to connect to the server with this code:
ws = create_connection("wss://127.0.0.1:9000")
What options do I need to add to create_connection? Adding sslopt={"cert_reqs": ssl.CERT_NONE} does not work:
websocket._exceptions.WebSocketBadStatusException: Handshake status 400
This works
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())
For me the option from the question seems to work:
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()
See also here:
https://github.com/websocket-client/websocket-client#faq
Note: I'm using win7 with python 3.6.5 with following packages installed(pip):
simple-websocket-server==0.4.0
websocket-client==0.53.0
websockets==6.0
Related
I'm connecting to the same websocket address but for some reason I can't receive data using websockets+asyncio.
However, it does work when using websocket as well as using a raw websocket connection with Postman.
if SOCKET is the address
Using websockets + asyncio
import asyncio
import websockets
SOCKET = 'wss://api...'
async def handle(uri):
async with websockets.connect(uri) as websocket:
while True:
print(await websocket.recv())
asyncio.get_event_loop().run_until_complete(handle(SOCKET))
Using websocket
from websocket import create_connection
ws = create_connection(SOCKET)
while True:
try:
result = ws.recv()
print(result)
Does anyone know what can possibly be wrong with the first option? I'm actually following a python sample request suggested by a site's documentation when using websockets+asyncio.
I try to connect to blockchain.com websocket by using this API. I use websockets library for Python. When I connect by using the interactive client method, I have a success. But when I try to connect by using my Python script, I have no any response from server socket. This is my script:
import asyncio
import websockets
async def main():
async with websockets.connect("wss://ws.blockchain.info/inv") as client:
print(await client.recv())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
This is what I have in terminal when I use interactive client, and I expect to see it when I run my script:
Connected to wss://ws.blockchain.info/inv.
>
As #SteffenUllrich mentioned in comment this text is displayed by interactive client - it is not message received from server - and you have to use own print("Connected to wss://ws.blockchain.info/inv.") to display it.
And if you want to recv() something from sever then first you have to send() command to server.
import asyncio
import websockets
async def main():
async with websockets.connect("wss://ws.blockchain.info/inv") as client:
print("[main] Connected to wss://ws.blockchain.info/inv" )
cmd = '{"op":"ping"}'
print('[main] Send:', cmd)
await client.send(cmd)
print('[main] Recv:', await client.recv())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Result:
[main] Connected to wss://ws.blockchain.info/inv
[main] Send: {"op":"ping"}
[main] Recv: {"op":"pong"}
I am trying to test out if I send out multiple requests at the same moment using coroutine can cause the server side receives corrupted data.
My test is based on the sample code from: https://websockets.readthedocs.io/en/stable/intro.html
Somehow, for the following code, the server side only receive one requests? Anyone has some insights? thx
server (this is basically the same code from the websockets Getting Started webpage):
#!/usr/bin/env python3
# WS server example
import asyncio
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f"> {greeting}")
start_server = websockets.serve(hello, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Client, I created 1000 tasks, and schedule to run them as soon as possible:
#!/usr/bin/env python3
# WS client example
import asyncio
import websockets
uri = "ws://localhost:8765"
connection = None
async def hello():
global connection
name = "What's your name? "
await connection.send(name)
print(f"> {name}")
async def main():
global connection
connection = await websockets.connect(uri)
#asyncio.run(main())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.run_until_complete(asyncio.wait(
[hello() for i in range(1000)], return_when=asyncio.ALL_COMPLETED
))
UPDATE
The solution is to use a loop.
I found the reason: the server side, handler should use a loop so that the corroutine will not finish immediately after received the first request.
The documentation you linked also includes this paragraph just below the server code:
On the server side, websockets executes the handler coroutine hello once for each WebSocket connection. It closes the connection when the handler coroutine returns.
The client code you linked creates one connection and sends messages on that connection. After the client sends the first message, the server closes the connection, so the next 999 messages you attempt to send are being sent on a closed connection.
If you update the hello handler to include a loop, you will see all messages.
import asyncio
import websockets
async def hello(websocket, path):
while True:
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f"> {greeting}")
start_server = websockets.serve(hello, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Im creating a websocket server in PYTHON
import asyncio
import pathlib
import ssl
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f"> {greeting}")
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
localhost_pem = pathlib.Path(__file__).with_name("localhost.pem")
ssl_context.load_cert_chain(localhost_pem)
start_server = websockets.serve(
hello, "localhost", 8765, ssl=ssl_context
)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
This is the client
import asyncio
import pathlib
import ssl
import websockets
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
localhost_pem = pathlib.Path(__file__).with_name("localhost.pem")
ssl_context.load_verify_locations(localhost_pem)
async def hello():
uri = "wss://psXXXXXX.dreamhostps.com:8765"
async with websockets.connect(
uri, ssl=ssl_context
) as websocket:
name = input("What's your name? ")
await websocket.send(name)
print(f"> {name}")
greeting = await websocket.recv()
print(f"< {greeting}")
asyncio.get_event_loop().run_until_complete(hello())
I start the python server in my host provider by running the server python file and there's no problem at all
But when I try to run my python client the SERVER refuses the connection. Everything seems to be right but Im not quite sure how to set up this localhost.pem for my VPS server in dreamhost.
Any guileline about how to do this would be much appreciated.
I'm trying to create a unidirectional websocket connection between the client and server (python). The libraries I've currently been prototyping with are websockets and simplesocketserver. I've got a hello world example getting from the server to the client, but I need to be able to send data from the backend to the client unprompted from the client. All of the websockets examples seem to show the server listening to the client and then responding.
So far I've tried:
Using websockets 8.0, sending data from server to client unprompted, this works but only with hard-coded strings, I don't understand how to send real data on demand unprompted from the client
Using simplesocketserver in the same exact manner
Started investigating server sent events - is this more appropriate?
Example from the websockets documentation:
import asyncio
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f"> {greeting}")
start_server = websockets.serve(hello, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Note: the need for unidirectional communication is due to existing architecture.
Any guidance or resources on this would be great, I hope I'm overlooking something easy. Thank you!
I came across a similar issue. After some trial and error I found a solution that works for me, maybe so it does for you. I've tested this on Linux and with Firefox and mobile Safari (parallel).
The InputThread waits for input (words) on the command line and writes them into a list, that is sent to all connected clients each time a new string is appended or a client is connected.
Code snippet
import asyncio
import websockets
import json
from threading import Thread
words = []
clients = []
async def register_client(websocket, path):
# register new client in list and keep connection open
clients.append(websocket)
await send_to_all_clients()
while True:
await asyncio.sleep(10)
async def send_to_all_clients():
global words
for i, ws in list(enumerate(clients))[::-1]:
try:
await ws.send(json.dumps({"words": words}))
except websockets.exceptions.ConnectionClosedError:
# remove if connection closed
del clients[i]
class InputThread(Thread):
def run(self):
global words
async def sending_loop():
while True:
i = input()
if not i.strip():
continue
words.append({"options": i.strip().slit()})
await send_to_all_clients()
asyncio.run(sending_loop())
InputThread().start()
asyncio.get_event_loop().run_until_complete(
websockets.serve(register_client, "localhost", 8765))
asyncio.get_event_loop().run_forever()