我正在使用提琴手作为代理,并尝试打开与应用程序的连接。 步骤如下:
Proxy Code:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
timeval = struct.pack('ll', 3, 0)
s.setsockopt(socket.SOL_SOCKET,socket.SO_RCVTIMEO, timeval)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.connect((ip_addr, port)) #Proxy connection
request_header = "CONNECT {0}:{1} HTTP/1.1\r\n".format(HOST, 443)
s.send(request_header)
request_header = "Host: {0}:{1}\r\n".format(HOST, 443)
s.send(request_header)
request_header = "Proxy-Connection: keep-alive\r\n"
s.send(request_header)
request_header = "\r\n"
s.send(request_header)
recvd = s.recv(1024)
HTTP / 1.1 200连接已建立
现在我正在使用此套接字连接到应用程序:
Code:
header_format ='''GET /db/cert/im/d HTTP/1.1\r
Host: {0}\r
Content-Type: text/html\r
Accept: <relevant details>\r
\r
'''
s_sock = ssl.wrap_socket(s, keyfile="test_cert.key", \
certfile="test_cert.pem", server_side=False, do_handshake_on_connect=True)
request_header = header_format.format(HOST)
s_sock.write(request_header)
data = s_sock.read()
print data
DATA:
HTTP / 1.0 401未经授权
WWW-Authenticate:基本领域=“”
服务器:SomeServer
连接:保持活动
内容长度:35
我想知道如何使用代理使用PKI安全性访问网站?
已更新 到目前为止,我正在尝试使用httplib和urllib2
proxy = urllib2.ProxyHandler({
'http': ip_port,
'https': ip_port
})
以下实现从此处复制:enter link description here
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def __init__(self, key, cert):
urllib2.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert
def https_open(self, req):
return self.do_open(self.getConnection, req)
def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host,
key_file=self.key,
cert_file=self.cert,
timeout=timeout)
def open_url(url, key, cert):
opener = urllib2.build_opener(HTTPSClientAuthHandler(key, cert))
urllib2.install_opener(opener)
opener.addheaders = [
("User-Agent", "<custom>"),
("Accept", "<custom>"),
]
response = urllib2.urlopen(url)
print response.read()
The above implementation works properly, as soon as I add proxy handler I get 401 error.
opener = urllib2.build_opener(proxy,HTTPSClientAuthHandler(key, cert))
File "test_proxy.py", line 54, in open_url
response = urllib2.urlopen(url)
File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
File "/usr/lib/python2.6/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.6/urllib2.py", line 435, in error
return self._call_chain(*args)
File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized