Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 - Requests with Sock5 proxy

Is there a way to use sock5 proxy to use TOR with requests? I know that requests only use http proxy...

import requests
r = requests.get('http://www.google.com',proxies= my_proxy)
like image 504
Bob Ebert Avatar asked Aug 02 '15 23:08

Bob Ebert


People also ask

How do you add a proxy to a request in Python?

To use a proxy in Python, first import the requests package. Next create a proxies dictionary that defines the HTTP and HTTPS connections. This variable should be a dictionary that maps a protocol to the proxy URL. Additionally, make a url variable set to the webpage you're scraping from.

Does SOCKS5 work with proxy?

Your traffic is routed through a proxy server that generates an arbitrary IP address before you reach your destination. Technically speaking, SOCKS5 (the latest version) uses proxy servers to form User Datagram Protocol (UDP) or Transmission Control Protocol (TCP) connections through arbitrary IP addresses.

Can I use SOCKS proxy for HTTP?

HTTP proxies can add a layer of security between the client and the server and can detect and deny suspicious data packets or spyware. SOCKS proxies do not directly use the HTTP protocol. It is commonly used for more general purposes such as content streaming and P2P file sharing.

How do I use a SOCKS5 proxy?

Click the Apple icon at the top left of the menu bar on your screen and select System Preferences. Select Network and then Proxies. Click the Advanced button to access the Network settings and navigate to the Proxies tab. Click the SOCKS Proxy checkbox and enter the host and port information.


2 Answers

You can use socks, socket modules

import socks
import socket
from urllib import request

socks.set_default_proxy(socks.SOCKS5, "localhost", 9050)
socket.socket = socks.socksocket
r = request.urlopen('http://icanhazip.com')
print(r.read()) # check ips

The socks package can be installed from multiple packages which are forks of socksipy. One particular one that also works on Python3 is PySocks. You can install it, for example, with pip:

pip3 install PySocks
like image 131
Eugene Alkhouski Avatar answered Oct 10 '22 17:10

Eugene Alkhouski


I think at the moment requests works with socks5 out of the box.

import requests

url = 'https://example.com'
proxies = {'http': 'socks5://127.0.0.1:9150',
           'https': 'socks5://127.0.0.1:9150'}
r = requests.get(url, proxies=proxies)
like image 21
michael Avatar answered Oct 10 '22 15:10

michael