Connect to a server through proxy with socket library (python3)

邮差的信 提交于 2019-12-12 03:04:01

问题


I'd like to use socket library to connect to a server (not necessary a webserver) through an http proxy. Is it possible?

For example (with requests library):

import requests

r = requests.get("http://www.google.com", #but not necessary to http port.
                 proxies={"http": "http://ipproxy:portproxy"})

-UPDATE-

import urllib.request

pr = "ipproxy:portproxy"

while True:
    try:
        proxy = urllib.request.ProxyHandler({'http': pr})
        opener = urllib.request.build_opener(proxy)
        urllib.request.install_opener(opener)
        url = "ftp://ip" # or ssh:// or some other port
        data = None
        headers = {}
        req = urllib.request.Request(url, data, headers)
        print ("Request sent")
    except:
        print ("An error occurred")

回答1:


It is possible for most types of connections (HTTP and FTP etc., possibly HTTPS, although this is a bit more tricky) using the urllib module with a ProxyHandler object.



来源:https://stackoverflow.com/questions/39066009/connect-to-a-server-through-proxy-with-socket-library-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!