从Tor继电器后面做http请求

时间:2022-02-03 10:37:55

I am trying to do a http request form behind a tor relay. I have a little script that logs in and gets data from a page like this:

我正在尝试在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("*****", "*****"))

The code above works if ran separately, I get a string of the pages content returned. But if I call it in the query function below (have been following the Russia tutorial) I get an error.

如果单独运行上面的代码,我得到一个返回的页面内容的字符串。但是如果我在下面的查询函数中调用它(一直在遵循俄罗斯教程),我会收到错误。

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

The error I ma getting when calling requestAccountData("*****", "*****") is:

调用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

I can not understand what the error is.

我无法理解错误是什么。

1 个解决方案

#1


First of all, the fresh SocksiPy module is available as a PySocks python package that would cover lots of problems.

首先,新的SocksiPy模块可用作PySocks python包,可以解决许多问题。

This example uses Monkeypatching, where the entire standard library with a single default proxy:

此示例使用Monkeypatching,其中整个标准库具有单个默认代理:

import socket
import socks
socks.set_default_proxy(socks.SOCKS5, "localhost")
socket.socket = socks.socksocket

So, when you try to execute socket.socket = socks.socksocket() it raises the error - TypeError: 'socksocket' object is not callable. Be that as it may, requests works well behind Tor.

因此,当您尝试执行socket.socket = socks.socksocket()时,会引发错误 - TypeError:'socksocket'对象不可调用。尽管如此,请求在Tor之后很有效。

#1


First of all, the fresh SocksiPy module is available as a PySocks python package that would cover lots of problems.

首先,新的SocksiPy模块可用作PySocks python包,可以解决许多问题。

This example uses Monkeypatching, where the entire standard library with a single default proxy:

此示例使用Monkeypatching,其中整个标准库具有单个默认代理:

import socket
import socks
socks.set_default_proxy(socks.SOCKS5, "localhost")
socket.socket = socks.socksocket

So, when you try to execute socket.socket = socks.socksocket() it raises the error - TypeError: 'socksocket' object is not callable. Be that as it may, requests works well behind Tor.

因此,当您尝试执行socket.socket = socks.socksocket()时,会引发错误 - TypeError:'socksocket'对象不可调用。尽管如此,请求在Tor之后很有效。