websockets_proxy
This module will enable you to use websockets package with proxies.
Proxy heavy-lifting is done by python-socks package.
Usage
To install, use:
pip install websockets_proxy
All you need is create a proxy object and pass it into proxy_connect constructor:
from websockets_proxy import Proxy, proxy_connect
proxy = Proxy.from_url("...")
async with proxy_connect("wss://example.com", proxy=proxy) as ws:
...
proxy_connect constructor accepts the same arguments as regular connect constructor,
with the addition of proxy and proxy_conn_timeout, which are self-explanatory.
With this patch you cannot use
sockkeyword argument, because it is used to connect to a proxy server.
You must create your
Proxyobjects within a running event loop (inside anasyncfunction). This is a python-socks limitation. If you define yourProxyobjects outside of running event loop, you'll get this kind of error:RuntimeError: Task <Task pending name=...> attached to a different loop.
WebSocket proxy checker
Server
Here is a simple aiohttp server, which allows you to check, if your proxy connections work as expected:
import asyncio
from aiohttp import web, WSMsgType, WSMessage
HOST = '0.0.0.0'
PORT = 9999
app = web.Application()
async def websocket_handler(request: web.Request) -> web.StreamResponse:
ws = web.WebSocketResponse()
await ws.prepare(request)
await ws.send_str(str(request.remote))
await ws.close()
print('accepted connection from', request.remote)
return ws
app.add_routes([
web.get('/', websocket_handler)
])
async def main():
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host=HOST, port=PORT)
await site.start()
print('server is running')
await asyncio.Future()
if __name__ == '__main__':
asyncio.run(main())
Client
An example of a client-side proxy checker script:
import asyncio
from websockets_proxy import Proxy, proxy_connect
from websockets import connect
# this script is written with the above checker server in mind
CHECKER_URL = 'ws://address:port'
async def main():
async with connect(CHECKER_URL) as ws:
async for msg in ws:
ip_no_proxy = msg
print("Your IP:", ip_no_proxy)
print('.')
# be sure to create your "Proxy" objects inside an async function
proxy = Proxy.from_url("http://login:password@address:port")
async with proxy_connect(CHECKER_URL, proxy=proxy) as ws:
async for msg in ws:
ip_with_proxy = msg
print("(async with) Proxy IP", ip_with_proxy)
print('.')
ws = await proxy_connect(CHECKER_URL, proxy=proxy)
async for msg in ws:
ip_with_proxy = msg
print("(await) Proxy IP", ip_with_proxy)
await ws.close()
print('.')
if __name__ == "__main__":
asyncio.run(main())
Metadata
Release files for websockets-proxy 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| websockets_proxy-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for websockets_proxy-0.1.3-py3-none-any.whl |
Release files / websockets_proxy-0.1.3-py3-none-any.whl
| Download URL | websockets_proxy-0.1.3-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
07ca0b561107e4e207c00a5c86f9281c05be2a3e34aa721d01743760dc5fd067
|
|
BLAKE2b-256 checksum How to use checksums |
e1b36cbfcd56d3445e0673ea7fda7b48feaf48868c65697dbd23272225a1df20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Security for websockets-proxy 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.