我正在尝试在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
我无法理解错误是什么。
答案 0 :(得分:1)
首先,新的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。