hackorama
hackorama

Open HTTPS URL Through an HTTP Proxy and Upload Multi Part MIME Data using Python

Shows how to securely upload a file using python script through a web proxy. I could not find a straightforward solution to this. I had to put together two different python recepies to get it to work.

First one solves using HTTPS through a web proxy.

Using ProxyHTTPConnection and ConnectHTTPSHandler by Alessandro Budai from:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/456195

Second one solves sending multi part form data for file upload.

Using MultipartPostHandler by Will Holcomb from:
http://odin.himinbi.org/MultipartPostHandler.py

Please see the final solution below, after going through different variations starting from simple http connection.

# HTTP example
import urllib2

response = urllib2.urlopen('http://www.webserver.com/test').read().strip()
print response 


# HTTPS example
import urllib2

response = urllib2.urlopen('https://www.webserver.com/test').read().strip()
print response 


# HTTP + MULTIPART example
import urllib2
import MultipartPostHandler

params = {'user':'foo','file':open('foo.jpg', 'rb')}
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
req = urllib2.Request('http://www.webserver.com/test', params)
response = urllib2.urlopen(req).read().strip()
print response 


# HTTPS + MULTIPART example
import urllib2
import MultipartPostHandler

params = {'user':'foo','file':open('foo.jpg', 'rb')}
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
req = urllib2.Request('https://www.webserver.com/test', params)
response = urllib2.urlopen(req).read().strip()
print response 


# HTTP + PROXY example
import urllib2

req = urllib2.Request('http://www.webserver.com/test')
req.set_proxy('proxyserver:8888', 'http')
response = urllib2.urlopen(req).read().strip()
print response 


# HTTPS + PROXY example
import urllib2
from ProxyHTTPConnection import ConnectHTTPSHandler

opener = urllib2.build_opener(ConnectHTTPSHandler)
urllib2.install_opener(opener)
req = urllib2.Request('https://www.webserver.com/test')
req.set_proxy('proxyserver:8888', 'https')
response = urllib2.urlopen(req).read().strip()
print response 


# HTTP + PROXY +  MULTIPART example
import urllib2
import MultipartPostHandler

params = {'user':'foo','file':open('foo.jpg', 'rb')}
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
req = urllib2.Request('http://www.webserver.com/test', params)
req.set_proxy('proxyserver:8888', 'http')
response = urllib2.urlopen(req).read().strip()
print response 


# HTTPS + PROXY + MULTIPART example
import urllib2
import MultipartPostHandler
from ProxyHTTPConnection import ConnectHTTPSHandler

params = {'user':'foo','file':open('foo.jpg', 'rb')}
opener = urllib2.build_opener(ConnectHTTPSHandler, 
            MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
req = urllib2.Request('https://www.webserver.com/test', params)
req.set_proxy('proxyserver:8888', 'https')
response = urllib2.urlopen(req).read().strip()
print response 

Local copy of MultipartPostHandler
Local copy of ProxyHTTPConnection



Friday, 25-Aug-2006 15:05:10 PDT kishan at hackorama dot com