python请求ssh隧道作为代理

时间:2018-10-23 22:32:40

标签: python ssh proxy python-requests tunnel

是否可以将带有ssh隧道的python请求用作代理?我该如何实现?已经尝试过但没有成功:

from sshtunnel import SSHTunnelForwarder
import requests as r
from bs4 import BeautifulSoup as soup

server = SSHTunnelForwarder(
        'sship',
        ssh_username="XXXX",
        ssh_password="XXXX",
        remote_bind_address=('127.0.0.1', 8080)
    )

server.start()

print(server.local_bind_port)


proxies = {
    "http": "http://127.0.0.1:8080",
    }

url = 'http://www.google.com'
headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': 'youremail@domain.com'  # This is another valid field
}

data = r.get(url, headers=headers , proxies = proxies)
page_data = soup(data.text , 'html.parser')

print page_data

这是我得到的错误:

37657 追溯(最近一次通话):   在第27行的文件“ ssh.py”中     数据= r.get(URL,标头=标头,代理=代理)   在get中的文件“ /usr/lib/python2.7/site-packages/requests-2.19.1-py2.7.egg/requests/api.py”,第72行     返回请求('get',url,params = params,** kwargs)   请求中的文件“ /usr/lib/python2.7/site-packages/requests-2.19.1-py2.7.egg/requests/api.py”,第58行     return session.request(method = method,url = url,** kwargs)   请求中的文件“ /usr/lib/python2.7/site-packages/requests-2.19.1-py2.7.egg/requests/sessions.py”,第512行     resp = self.send(准备,** send_kwargs)   发送中的文件“ /usr/lib/python2.7/site-packages/requests-2.19.1-py2.7.egg/requests/sessions.py”,第622行     r = adapter.send(request,** kwargs)   发送中的文件“ /usr/lib/python2.7/site-packages/requests-2.19.1-py2.7.egg/requests/adapters.py”,第507行     引发ProxyError(e,request = request) requests.exceptions.ProxyError:HTTPConnectionPool(host ='127.0.0.1',端口= 80):url:http://google.com/超过了最大重试次数(由ProxyError('无法连接到代理服务器。',NewConnectionError(':失败)引起建立新的连接:[Errno 111]连接被拒绝',)))

1 个答案:

答案 0 :(得分:0)

我认为您需要在SSHTunnel中显式设置本地绑定地址,或者像这样设置代理的端口:

`
https_tunnel.start()
    proxies = {
        #'http': 'http://localhost:{}'.format(str(http_tunnel.local_bind_port)),
        'https': 'https://localhost:{}'.format(str(https_tunnel.local_bind_port))
    }
`

其中,https_tunnel是SSHTunnelForwarder的实例。我已经做到了,但是在实际发送GET请求时出现错误:

  发送中的

python2.7 / site-packages / requests / adapters.py”,第490行       引发ConnectionError(err,request = request)requests.exceptions.ConnectionError:('Connection aborted。',   BadStatusLine(“''”,))