1

I currently use this for my connection to a socks5 proxy and paramiko.

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,socks_hostname,socks_port, True, socks_username,socks_password)
paramiko.client.socket.socket = socks.socksocket
ssh = paramiko.SSHClient()

However, I was hoping to make some requests in python with requesocks and the same proxy settings for the paramiko and couldn't find anything talking about a username and password.

Additionally, all requests are done with a different socks connection each time, Global settings could get in the way of my other connections.

Any ideas on how this is done or if there is an alternative to this? My current implementation uses python requests very heavily so it would be nice to transition from there to requesocks so I don't have to refactor everything.

Note: How to make python Requests work via socks proxy doesn't work as it doesn't use the socks5 authentication.

Community
  • 1
  • 1
Encompass
  • 443
  • 1
  • 13
  • 23
  • does [this answer from the question you've linked](http://89.169.229.68:8001) work for you? – jfs Nov 29 '15 at 16:30

2 Answers2

10

You can use PySocks:

pip install PySocks

Then in your python file:

import socket
import socks
import requests

socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9050, True, 'socks5_user','socks_pass')
socket.socket = socks.socksocket
print(requests.get('http://ifconfig.me/ip').text)

It works for me. But I got another problem to use different socks5 proxy for different request sessions. If anyone has solution for this, please kindly contribute.

ilovepython
  • 116
  • 1
  • 3
  • As you have said this sets the environment globally for my application. This is a background task that has to make request to open a proxy connection to the outside world, so each request is a new proxy connection. This would work otherwise. – Encompass Nov 30 '15 at 19:10
4

The modern way:

pip install -U requests[socks]

then

import requests

resp = requests.get('http://go.to', 
                    proxies=dict(http='socks5://user:pass@host:port',
                                 https='socks5://user:pass@host:port'))
dvska
  • 1,719
  • 1
  • 13
  • 14