从 Tor 中继后面做 http 请求
Doing http request from behind a Tor relay
我正在尝试在 tor 中继后面做一个 http 请求表单。我有一个小脚本可以登录并从这样的页面获取数据:
import requests
def requestAccountData(accountName, accountPass):
print("requesting data")
credentials = {'loginname': accountName, 'loginpassword': accountPass}
s = requests.Session()
s.post('https://example.com/account/', data=credentials)
button = { 'page': 'statistics' }
r = s.post('https://example.com/account/', data=button)
return r.text
#print(requestAccountData("*****", "*****"))
如果 运行 分开,上面的代码有效,我得到返回的页面内容的字符串。但是如果我在下面的查询函数中调用它(一直在关注俄罗斯教程)我会得到一个错误。
import socket
import socks # SocksiPy module
import stem.process
from stem.util import term
from requestAccountData import requestAccountData
SOCKS_PORT = 7000
# Set socks proxy and wrap the urllib module
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket()
# Perform DNS resolution through the socket
def getaddrinfo(*args):
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.getaddrinfo = getaddrinfo
def query(url):
"""
Uses requests to fetch a site using SocksiPy for Tor over the SOCKS_PORT.
"""
r = requestAccountData("*****", "*****")
print(r)
try:
return r
except:
return "Unable to reach %s" % url
# Start an instance of Tor configured to only exit through Russia. This prints
# Tor's bootstrap information as it starts. Note that this likely will not
# work if you have another Tor instance running.
def print_bootstrap_lines(line):
if "Bootstrapped " in line:
print(line)
print(term.format("Starting Tor:\n", term.Attr.BOLD))
tor_process = stem.process.launch_tor_with_config(
config = {
'SocksPort': str(SOCKS_PORT),
'ExitNodes': '{ru}',
},
init_msg_handler = print_bootstrap_lines,
)
print("Checking our endpoint:")
print(query("https://www.atagar.com/echo.php"))
tor_process.kill() # stops tor
我在调用 requestAccountData("*****", "*****")
时遇到的错误是:
Traceback (most recent call last):
File "C:\Users\gatsu\My Documents\LiClipse Workspace\TorCommunicator\Communicator.py", line 56, in <module>
print(query("https://www.atagar.com/echo.php"))
File "C:\Users\gatsu\My Documents\LiClipse Workspace\TorCommunicator\Communicator.py", line 26, in query
r = requestAccountData("*****", "*****")
File "C:\Users\gatsu\My Documents\LiClipse Workspace\TorCommunicator\requestAccountData.py", line 8, in requestAccountData
s.post('https://example.com/account/', data=credentials)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\sessions.py", line 508, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\adapters.py", line 370, in send
timeout=timeout
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connectionpool.py", line 341, in _make_request
self._validate_conn(conn)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connectionpool.py", line 762, in _validate_conn
conn.connect()
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connection.py", line 204, in connect
conn = self._new_conn()
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connection.py", line 134, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\util\connection.py", line 68, in create_connection
sock = socket.socket(af, socktype, proto)
TypeError: 'socksocket' object is not callable
我不明白错误是什么。
首先,新的 SocksiPy 模块作为 PySocks
python 包提供,可以解决很多问题。
此示例使用 Monkeypatching
,其中整个标准库具有一个默认代理:
import socket
import socks
socks.set_default_proxy(socks.SOCKS5, "localhost")
socket.socket = socks.socksocket
因此,当您尝试执行 socket.socket = socks.socksocket()
时,它会引发错误 - TypeError: 'socksocket' object is not callable
。尽管如此,requests
在 Tor 后面运行良好。
我正在尝试在 tor 中继后面做一个 http 请求表单。我有一个小脚本可以登录并从这样的页面获取数据:
import requests
def requestAccountData(accountName, accountPass):
print("requesting data")
credentials = {'loginname': accountName, 'loginpassword': accountPass}
s = requests.Session()
s.post('https://example.com/account/', data=credentials)
button = { 'page': 'statistics' }
r = s.post('https://example.com/account/', data=button)
return r.text
#print(requestAccountData("*****", "*****"))
如果 运行 分开,上面的代码有效,我得到返回的页面内容的字符串。但是如果我在下面的查询函数中调用它(一直在关注俄罗斯教程)我会得到一个错误。
import socket
import socks # SocksiPy module
import stem.process
from stem.util import term
from requestAccountData import requestAccountData
SOCKS_PORT = 7000
# Set socks proxy and wrap the urllib module
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket()
# Perform DNS resolution through the socket
def getaddrinfo(*args):
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.getaddrinfo = getaddrinfo
def query(url):
"""
Uses requests to fetch a site using SocksiPy for Tor over the SOCKS_PORT.
"""
r = requestAccountData("*****", "*****")
print(r)
try:
return r
except:
return "Unable to reach %s" % url
# Start an instance of Tor configured to only exit through Russia. This prints
# Tor's bootstrap information as it starts. Note that this likely will not
# work if you have another Tor instance running.
def print_bootstrap_lines(line):
if "Bootstrapped " in line:
print(line)
print(term.format("Starting Tor:\n", term.Attr.BOLD))
tor_process = stem.process.launch_tor_with_config(
config = {
'SocksPort': str(SOCKS_PORT),
'ExitNodes': '{ru}',
},
init_msg_handler = print_bootstrap_lines,
)
print("Checking our endpoint:")
print(query("https://www.atagar.com/echo.php"))
tor_process.kill() # stops tor
我在调用 requestAccountData("*****", "*****")
时遇到的错误是:
Traceback (most recent call last):
File "C:\Users\gatsu\My Documents\LiClipse Workspace\TorCommunicator\Communicator.py", line 56, in <module>
print(query("https://www.atagar.com/echo.php"))
File "C:\Users\gatsu\My Documents\LiClipse Workspace\TorCommunicator\Communicator.py", line 26, in query
r = requestAccountData("*****", "*****")
File "C:\Users\gatsu\My Documents\LiClipse Workspace\TorCommunicator\requestAccountData.py", line 8, in requestAccountData
s.post('https://example.com/account/', data=credentials)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\sessions.py", line 508, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\adapters.py", line 370, in send
timeout=timeout
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connectionpool.py", line 341, in _make_request
self._validate_conn(conn)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connectionpool.py", line 762, in _validate_conn
conn.connect()
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connection.py", line 204, in connect
conn = self._new_conn()
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\connection.py", line 134, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "C:\Python34\lib\site-packages\requests-2.6.0-py3.4.egg\requests\packages\urllib3\util\connection.py", line 68, in create_connection
sock = socket.socket(af, socktype, proto)
TypeError: 'socksocket' object is not callable
我不明白错误是什么。
首先,新的 SocksiPy 模块作为 PySocks
python 包提供,可以解决很多问题。
此示例使用 Monkeypatching
,其中整个标准库具有一个默认代理:
import socket
import socks
socks.set_default_proxy(socks.SOCKS5, "localhost")
socket.socket = socks.socksocket
因此,当您尝试执行 socket.socket = socks.socksocket()
时,它会引发错误 - TypeError: 'socksocket' object is not callable
。尽管如此,requests
在 Tor 后面运行良好。