Skip to content
Advertisement

Can’t send requests through socks5 proxy with Python

I was trying to send http/https requests via proxy (socks5), but I can’t understand if the problem is in my code or in the proxy.

I tried using this code and it gives me an error:

JavaScript

This is my code:

JavaScript

Then, I checked the proxy with an online tool The tool manages to send requests through the proxy. Online tool output.

So the problem is in this code? I can’t figure out what’s wrong.

Edit (15/09/2021)

I added headers but the problem is still there.

Advertisement

Answer

Create a local server/mock to handle the request using pytest or some other testing framework with responses library to eliminate variables external to your application/script. I’m quite sure Google will reject requests with empty headers. Also, ensure you installed the correct dependencies to enable SOCKS proxy support in requests (python -m pip install requests[socks]). Furthermore, if you are making a remote request to connect to your proxy you must change socks5 to socks5h in your proxies dictionary.

References

pytest: https://docs.pytest.org/en/6.2.x/

responses: https://github.com/getsentry/responses

requests[socks]: https://docs.python-requests.org/en/master/user/advanced/#socks

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement