gpt4 book ai didi

python - 通过经过身份验证的 HTTP 代理建立 TCP 连接?

转载 作者:行者123 更新时间:2023-11-28 22:58:50 25 4
gpt4 key购买 nike

我所在的网络要求我通过经过身份验证的 HTTP 代理进行连接才能访问网络外部的任何内容。我需要做的基本上是制作一个 socket(或等效的)来连接到 Internet,但通过代理发送所有数据而不是尝试直接发送。关于如何做到这一点有什么想法吗?

最佳答案

由于我没有找到任何我可以使用的实际模块或其他代码,所以我最终编写了自己的通过代理连接的函数:

def http_proxy_connect(address, proxy = None, auth = None, headers = {}):
"""
Establish a socket connection through an HTTP proxy.

Arguments:
address (required) = The address of the target
proxy (def: None) = The address of the proxy server
auth (def: None) = A tuple of the username and password used for authentication
headers (def: {}) = A set of headers that will be sent to the proxy

Returns:
A 3-tuple of the format:
(socket, status_code, headers)
Where `socket' is the socket object, `status_code` is the HTTP status code that the server
returned and `headers` is a dict of headers that the server returned.
"""
import socket
import base64

def valid_address(addr):
""" Verify that an IP/port tuple is valid """
return isinstance(addr, (list, tuple)) and len(addr) == 2 and isinstance(addr[0], str) and isinstance(addr[1], (int, long))

if not valid_address(address):
raise ValueError('Invalid target address')

if proxy == None:
s = socket.socket()
s.connect(address)
return s, 0, {}

if not valid_address(proxy):
raise ValueError('Invalid proxy address')

headers = {
'host': address[0]
}

if auth != None:
if isinstance(auth, str):
headers['proxy-authorization'] = auth
elif auth and isinstance(auth, (tuple, list)):
if len(auth) == 1:
raise ValueError('Invalid authentication specification')

t = auth[0]
args = auth[1:]

if t.lower() == 'basic' and len(args) == 2:
headers['proxy-authorization'] = 'Basic ' + base64.b64encode('%s:%s' % args)
else:
raise ValueError('Invalid authentication specification')
else:
raise ValueError('Invalid authentication specification')

s = socket.socket()
s.connect(proxy)
fp = s.makefile('r+')

fp.write('CONNECT %s:%d HTTP/1.0\r\n' % address)
fp.write('\r\n'.join('%s: %s' % (k, v) for (k, v) in headers.items()) + '\r\n\r\n')
fp.flush()

statusline = fp.readline().rstrip('\r\n')

if statusline.count(' ') < 2:
fp.close()
s.close()
raise IOError('Bad response')
version, status, statusmsg = statusline.split(' ', 2)
if not version in ('HTTP/1.0', 'HTTP/1.1'):
fp.close()
s.close()
raise IOError('Unsupported HTTP version')
try:
status = int(status)
except ValueError:
fp.close()
s.close()
raise IOError('Bad response')

response_headers = {}

while True:
tl = ''
l = fp.readline().rstrip('\r\n')
if l == '':
break
if not ':' in l:
continue
k, v = l.split(':', 1)
response_headers[k.strip().lower()] = v.strip()

fp.close()
return (s, status, response_headers)

关于python - 通过经过身份验证的 HTTP 代理建立 TCP 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13480141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com