armooo/cloudprint — Issue #22: Did not work with proxy 
Author: danielgeraldschroeder
Created: 2012-01-17 21:27:01+00:00
State: open

The box I wanted to use as a server lived behind a firewall.

So I need to make the following changes

Added code to handle this on my own box.

Likely should have a parameter for proxy.

Hopefully will help other with same issue.

file: cloudprint.py

...
SOURCE = 'Armooo-PrintProxy-1'
PRINT_CLOUD_SERICE_ID = 'cloudprint'
CLIENT_LOGIN_URL = '/accounts/ClientLogin'
PRINT_CLOUD_URL = '/cloudprint/'
*** ADDED >
BASE_GOOGLE_URL = 'https://www.google.com'
PROXY_HOST = 'proxy2.hh.chs'
<< END
class CloudPrintProxy(object):

def __init__(self, verbose=True):
...
username = raw_input('Google username: ')
password = getpass.getpass()
*** MODIFIED FROM >
r = rest.REST('https://www.google.com', debug=False)
*** MODIFIED TO>
r = rest.REST(BASE_GOOGLE_URL, debug=False, pHost=PROXY_HOST)
<< END
try:
auth_response = r.post(
CLIENT_LOGIN_URL,
...
return r
return f

auth = self.get_auth()
*** MODIFIED FROM >
return check_new_auth(rest.REST('https://www.google.com', auth=auth, debug=False))
*** MODIFIED TO>
return check_new_auth(rest.REST(BASE_GOOGLE_URL, auth=auth, debug=False, pHost=PROXY_HOST))
<< END

def get_printers(self):
r = self.get_rest()
printers = r.post(
....

file: rest.py

import httplib
import json
*** ADDED >
import socket
<< END
import ssl
import urllib
*** ADDED >
import urllib2
import urlparse
<< END
import UserDict
import UserList

...

type([]) : UserList.UserList,
type('') : UserString.UserString,
type(u'') : UserString.UserString,
}

*** ADDED >
def createConnection(self, host, pHost, pPort, pHostS, pPortS, https):
if https:
self._conn = httplib.HTTPSConnection(host)
port = 443
if not pHostS is None:
pHost = pHostS
if pportS != -1:
pPort = pPortS
else:
port = 80
self._conn = httplib.HTTPConnection(host)

if phost:
proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n'%(host,port)
user_agent = 'User-Agent: python\r\n'
proxy_pieces = proxy_connect+user_agent+'\r\n'
#now connect, very simple recv and error checking
proxy = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
proxy.connect((pHost,pPort))
proxy.sendall(proxy_pieces)
response = proxy.recv(8192)
status = response.split()[1]

if status != str(200):
raise 'Error status=',str(status)

if https:
self._conn.sock = ssl.wrap_socket(proxy, None, None)
else:
self._conn.sock = proxy
<< END

*** MODIFIED FROM >
def __init__(self, host, auth=None, debug=False):
*** MODIFIED TO>
def __init__(self, host, auth=None, debug=False, pHost=None, pPort=8080, pHostS=None, pPortS=-1):
<< END
proto, host = host.split('://')
if proto == 'https':
*** MODIFIED FROM >
self._conn = httplib.HTTPSConnection(host)
else:
self._conn = httplib.HTTPConnection(host)
*** MODIFIED TO>
https = True
else
https = False

self.createConnection(host, pHost=pHost, pPort=pPort, pHostS, pPortS, https)
<< END
self.debug = debug
if debug:
self._conn.set_debuglevel(10)
else:
self._conn.set_debuglevel(0)