I need help to connection websocket in python3 - python

I try connect to websocket but i have error:
server rejected WebSocket connection: HTTP 403
but when i try in website it's run like this website:
http://www.epetool.com/websocket/
the code in python 3.9:
import asyncio
import websockets
async def handler(websocket):
while True:
message = await websocket.recv()
print(message)
async def main():
url = "wss://io.dexscreener.com/dex/screener/pairs/h6/1?rankBy[key]=volume&rankBy[order]=desc&filters[buys][h1][min]=100&filters[sells][h1][min]=100&filters[liquidity][min]=500&filters[pairAge][max]=1&filters[chainIds][0]=bsc"
async with websockets.connect(url) as ws:
await handler(ws)
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())

Related

Python websockets connecting through the internet using IPV6

I want to send messages between two PCs accessing the internet through different networks. I have already created a firewall rule for the port in the server PC. The code works when both of them are using the same WiFi but not when using different connections.
Server (PC #1)
import asyncio
import websockets
import socket
async def hello(websocket):
name = await websocket.recv()
print(f"<<< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f">>> {greeting}")
async def main():
async with websockets.serve(hello, "", port=8765):
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
Client (PC #2)
import asyncio
import websockets
import socket
async def hello():
uri = "ws://[2a02:8388:8bc7:4d00:1921:8126:966d:8d32]:8765"
#not my real IP
async with websockets.connect(uri) as websocket:
name = input("What's your name? ")
await websocket.send(name)
print(f">>> {name}")
greeting = await websocket.recv()
print(f"<<< {greeting}")
if __name__ == "__main__":
asyncio.run(hello())

Websocket getting closed immediately after connecting to FastAPI Endpoint

I'm trying to connect a websocket aiohttp client to a fastapi websocket endpoint, but I can't send or recieve any data because it seems that the websocket gets closed immediately after connecting to the endpoint.
server
import uvicorn
from fastapi import FastAPI, WebSocket
app = FastAPI()
#app.websocket('/ws')
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
...
if __name__ == '__main__':
uvicorn.run('test:app', debug=True, reload=True)
client
import aiohttp
import asyncio
async def main():
s = aiohttp.ClientSession()
ws = await s.ws_connect('ws://localhost:8000/ws')
while True:
...
asyncio.run(main())
When I try to send data from the server to the client when a connection is made
server
#app.websocket('/ws')
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
await websocket.send_text('yo')
client
while True:
print(await ws.receive())
I always get printed in my client's console
WSMessage(type=<WSMsgType.CLOSED: 257>, data=None, extra=None)
While in the server's debug console it says
INFO: ('127.0.0.1', 59792) - "WebSocket /ws" [accepted]
INFO: connection open
INFO: connection closed
When I try to send data from the client to the server
server
#app.websocket('/ws')
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
await websocket.receive_text()
client
ws = await s.ws_connect('ws://localhost:8000/ws')
await ws.send_str('client!')
Nothing happens, I get no message printed out in the server's console, just the debug message saying the client got accepted, connection opened and closed again.
I have no idea what I'm doing wrong, I followed this tutorial in the fastAPI docs for a websocket and the example there with the js websocket works completely fine.
The connection is closed by either end (client or server), as shown from your code snippets. You would need to have a loop in both the server and the client for being able to await for messages, as well as send messages, continuously (have a look here and here).
Additionally, as per FastAPI's documentation:
When a WebSocket connection is closed, the await websocket.receive_text() will raise a WebSocketDisconnect
exception, which you can then catch and handle like in this example.
Thus, on server side, you should use a try-except block to catch and handle WebSocketDisconnect exceptions. Below is a working example demonstrating a client (in aiohttp) - server (in FastAPI) communication using websockets.
Working Example
Server
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
import uvicorn
app = FastAPI()
#app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
# await for connections
await websocket.accept()
try:
# send "Connection established" message to client
await websocket.send_text("Connection established!")
# await for messages and send messages
while True:
msg = await websocket.receive_text()
if msg.lower() == "close":
await websocket.close()
break
else:
print(f'CLIENT says - {msg}')
await websocket.send_text(f"Your message was: {msg}")
except WebSocketDisconnect:
print("Client disconnected")
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
Client
import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
async with session.ws_connect('ws://127.0.0.1:8000/ws') as ws:
# await for messages and send messages
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
print(f'SERVER says - {msg.data}')
text = input('Enter a message: ')
await ws.send_str(text)
elif msg.type == aiohttp.WSMsgType.ERROR:
break
asyncio.run(main())

Python Websockets: Why can't the client connect to my server

I'm using the websockets Python library.
So I have a client which simply is
# client.py
import websockets
import asyncio
async def main():
print("SENDING")
async with websockets.connect(f"ws://{IP}:8765") as connection:
await connection.send("Poggers")
print("END")
if __name__ == "__main__":
asyncio.run(main())
where IP is the IPv4 address I get when I visit https://whatismyipaddress.com.
My server code is
# server.py
import asyncio
import websockets
async def handle_server(websocket, path):
async for message in websocket:
print(message)
async def main():
async with websockets.serve(handle_server, "0.0.0.0", 8765):
await asyncio.Future()
if __name__ == "__main__":
asyncio.run(main())
Say I'm running the server code (server.py) and my friend's running the client code (client.py), in that order. For some reason, all he sees on his screen is
SENDING
and it hangs.
I see nothing on my screen.
Why is that?

Python websockets fast one way but 10x slower with response

I have a sanic webserver running websockets, behind the scenes its using the "websockets" library.
Server
from asyncio import sleep
from sanic import Sanic
app = Sanic("websocket test")
#app.websocket("/")
async def test(_, ws):
while True:
data = await ws.recv()
data = await ws.send('Hi')
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8000)
Client
import asyncio
import websockets
async def hello():
uri = "ws://localhost:8000"
async with websockets.connect(uri) as websocket:
while iteration:
await websocket.send("Hi")
await websocket.recv()
asyncio.get_event_loop().run_until_complete(hello())
When I remove ws.send('Hi') from the server and await websocket.recv() from the client i can get 58000 messages a second, once I start listening for a response it goes all the way down to 6000 messages a second, I am just curious what is making this run 10x slower when the server responds.
I think the solution here would be to seperate your send and recv into seperate tasks so they can yield concurrently.
async def producer(send):
while True:
await send("...")
await asyncio.sleep(1)
async def consumer(recv):
while True:
message = await recv
print(message)
async def test(request, ws):
request.app.add_task(producer(ws.send)))
request.app.add_task(consumer(ws.recv))
Obviously, this is a very simple example, and at the very least you should use some sort of a Queue for your producer.
But, when you break them into seperate tasks, then your recv is not blocked by send.

Python Websocket only receives once from client

I want to send values from a for loop from the client-server but the server only receives the first value and the connection is cut shortly
Client
import asyncio
import websockets
import time
async def message():
async with websockets.connect("ws://-------:5051") as socket:
for i in range(20):
await socket.send(f"{i}")
print(i)
time.sleep(4)
asyncio.get_event_loop().run_until_complete(message())
Server
import asyncio
import websockets
async def consumer_handler(websocket,path):
client_type = await websocket.recv()
print(client_type)
start_server = websockets.serve(consumer_handler,"ws://-------:5051", 5051)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
So, your consumer_handler receives message once and finishes.
You need to add loop.
Try something like this:
async def consumer_handler(websocket, path):
async for msg in websocket:
print(msg)

Categories