通过具有套接字库的代理(python3)连接到服务器 [英] Connect to a server through proxy with socket library (python3)

查看:268
本文介绍了通过具有套接字库的代理(python3)连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用套接字库通过http代理连接到服务器(不一定是网络服务器)。可能吗?

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

例如(带有请求库):

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")


推荐答案

对于大多数类型的连接(HTTP和FTP等,可能是HTTPS,尽管使用起来比较棘手),使用 URLlib 模块和 ProxyHandler 对象。

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.

这篇关于通过具有套接字库的代理(python3)连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆