Route PSX requests through a proxy
From version 1.2.0, psxdata can send every request it makes to PSX through a proxy you choose. That includes the data requests, the page fetches, and the fetch of theX-Req-Id request token that PSX requires on its data endpoints.
Typical reasons to use a proxy:
- Keep your own IP out of it. Scrapes from your laptop or a scheduled job don’t come from your personal connection.
- Isolate blocks. If an IP ever gets blocked, only the proxy’s egress is affected, not everything else on your network or cloud egress.
- Corporate networks. Some environments can only reach external sites through a proxy.
A proxy changes where requests come from, not how many are sent. psxdata’s
built-in rate limiting and request-token handling work exactly the same
with or without a proxy.
Installation
HTTP and HTTPS proxies work with the standard install. SOCKS proxies need the optionalsocks extra:
socks5:// URL without the extra installed, psxdata raises an ImportError that tells you what to install.
Per client
Passproxy to PSXClient. It applies to every request that client makes.
Module-level functions
psxdata.stocks(), psxdata.quote(), and the other module-level functions share a default client. Use psxdata.configure() to give it a proxy:
psxdata.configure() with no arguments resets the default client to no proxy.
Separate proxies per scheme
To use different proxies forhttp and https traffic, pass a dict. It has the same shape as the proxies dict in requests:
Supported proxy URLs
Credentials go in the URL as
scheme://user:pass@host:port. Percent-encode special characters in the password (for example @ becomes %40).
The URL must include a scheme: proxy.example.com:8080 on its own is rejected with a ValueError. An invalid proxy is rejected when the client is created, not at the first request.
The request token uses the same proxy
PSX requires anX-Req-Id token on its data requests, and psxdata fetches it from a PSX page automatically. The token may be tied to the client’s IP address, so psxdata always fetches it through the same proxy as the data requests that use it. Clients that share a proxy configuration also share a token. A client without a proxy uses the normal process-wide token.
Environment variables
With noproxy argument, psxdata behaves exactly as before: the standard HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables are still honoured, as with any requests-based library.
An explicit proxy takes precedence over those environment variables, and it only applies to psxdata, not to the rest of your process.
Confirming the proxy is used
Enable debug logging for psxdata. Every request logs the proxy it goes through:Errors
If the proxy can’t be reached, or it fails on every retry, psxdata raisesPSXConnectionError, the same as for any other network failure. The message names the proxy, with credentials hidden:
See also
PSXClientandpsxdata.configure()— full parameter reference- Exceptions — the error hierarchy