我想手动(使用socket and ssl模块)制作一个HTTPS
通过本身使用的代理请求HTTPS
.
我可以执行初始操作CONNECT
交换就好:
import ssl, socket
PROXY_ADDR = ("proxy-addr", 443)
CONNECT = "CONNECT example.com:443 HTTP/1.1\r\n\r\n"
sock = socket.create_connection(PROXY_ADDR)
sock = ssl.wrap_socket(sock)
sock.sendall(CONNECT)
s = ""
while s[-4:] != "\r\n\r\n":
s += sock.recv(1)
print repr(s)
上面的代码打印HTTP/1.1 200 Connection established
加上一些标题,这正是我所期望的。所以现在我应该准备好提出请求,例如
sock.sendall("GET / HTTP/1.1\r\n\r\n")
但上面的代码返回
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
Reason: You're speaking plain HTTP to an SSL-enabled server port.<br />
Instead use the HTTPS scheme to access this URL, please.<br />
</body></html>
这也很有意义,因为我仍然需要与example.com
我正在通过隧道连接到的服务器。但是,如果不是立即发送GET
要求我说
sock = ssl.wrap_socket(sock)
与远程服务器握手,然后我得到一个异常:
Traceback (most recent call last):
File "so_test.py", line 18, in <module>
ssl.wrap_socket(sock)
File "/usr/lib/python2.6/ssl.py", line 350, in wrap_socket
suppress_ragged_eofs=suppress_ragged_eofs)
File "/usr/lib/python2.6/ssl.py", line 118, in __init__
self.do_handshake()
File "/usr/lib/python2.6/ssl.py", line 293, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
那么我怎样才能与远程进行SSL握手呢?example.com
server?
编辑:我很确定在我第二次致电之前没有可用的其他数据wrap_socket
因为打电话sock.recv(1)
无限期地阻止。