Last active
March 23, 2016 12:16
-
-
Save jkryanchou/e95d6ef76fbc2ed8d395 to your computer and use it in GitHub Desktop.
ProxyMesh Requests HTTPS Code Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
import re | |
import base64 | |
import socket | |
import requests.packages.urllib3.connectionpool | |
try: | |
from http.client import HTTPConnection, HTTPSConnection | |
except ImportError: | |
from httplib import HTTPConnection, HTTPSConnection | |
PROXY_BASIC_AUTH = "Basic " + base64.encodestring("username:password") | |
PROXY_MESH_IP_PATTERN = re.compile("HTTP/1.1 200 Connection established\r\nX-ProxyMesh-IP: ([0-9]+(?:\.[0-9]+){3})") | |
class MyHTTPConnection(HTTPConnection): | |
def connect(self): | |
self.sock = sock_patch | |
class MyHTTPSConnection(HTTPSConnection): | |
def connect(self): | |
self.sock = sock_patch | |
requests.packages.urllib3.connectionpool.HTTPSConnection = MyHTTPSConnection | |
requests.packages.urllib3.connectionpool.HTTPConnection = MyHTTPConnection | |
if __name__ == '__main__': | |
proxy_host, proxy_port = "us-ca.proxymesh.com", 31280 | |
sock_patch = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock_patch.connect((proxy_host, proxy_port)) | |
CONNECT = "CONNECT {}:{} HTTP/1.1\r\nHost: {}:{}\r\nProxy-Authorization: {}\r\n".format("github.com", 443, 'github.com', 443, PROXY_BASIC_AUTH) | |
sock_patch.send(CONNECT) | |
result = sock_patch.recv(4096) | |
print result | |
resp = requests.get("https://github.com/url") | |
print resp, resp.headers | |
print resp.request.headers | |
print resp.text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment