Python3 - 使用 Sock5 代理的请求 [英] Python3 - Requests with Sock5 proxy

查看:112
本文介绍了Python3 - 使用 Sock5 代理的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 sock5 代理在请求中使用 TOR?我知道请求只使用 http 代理...

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)

推荐答案

你可以使用socks, socket模块

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

socks 包可以从多个包安装,这些包是 socksipy 的分支.一个也适用于 Python3 的特殊工具是 PySocks.例如,您可以使用 pip 安装它:

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

这篇关于Python3 - 使用 Sock5 代理的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆