An SSL error occurred using the HTTPS proxy On Python Request

Viewed 1656

When I do not use the HTTPS proxy (special), everything is normal! But use "requests" to return errors with the proxy HTTP:

proxies = {
    'http':"http://175.172.81.174:5324",
    'https':"http://175.172.81.174:5324",
}
import requests
r = requests.get("https://weibo.com/signup/signup.php",proxies=proxies,verify=False)

HTTPSConnectionPool(host='weibo.com', port=443): Max retries exceeded with url: /signup/signup.php (Caused by SSLError(SSLError(1, u'[SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:661)'),))

When replaced with the following code, it works:

from OpenSSL import SSL

import socket
proxies = {
    'http':"http://175.172.81.174:5324",
    'https':"http://175.172.81.174:5324",
}
headers = """GET https://weibo.com/signup/signup.php HTTP/1.1
Host: weibo.com
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: https://weibo.com/
Accept-Language: zh-CN,zh;q=0.8\r\n\r\n
"""
def verify_cb(conn, cert, errun, depth, ok):
        return True

server = 'weibo.com'
port = 443
PROXY_ADDR = ("175.172.81.174", 5324)
CONNECT = "CONNECT %s:%s HTTP/1.1\r\nConnection: close\r\n\r\n" % (server, port)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(PROXY_ADDR)
s.send(CONNECT)
print s.recv(4096)      

ctx = SSL.Context(SSL.TLSv1_METHOD)
ctx.set_verify(SSL.VERIFY_PEER, verify_cb)
ss = SSL.Connection(ctx, s)

ss.set_connect_state()
ss.do_handshake()
cert = ss.get_peer_certificate()
print cert.get_subject()
ss.send(headers)
print ss.recv(3000)
ss.shutdown()
ss.close()

How do I make "request" work?

My system is Windows python2.7.13

0 Answers
Related