Skip to main content.
Septiembre 22nd, 2007  español 

libgmail throught HTTP(S) proxy

Computer Science Programming

libgmail is, in words of its author, a pure python binding to provide access to your gmail account.

I’m using it and find it very useful. However, within my organization, I must connect to the internet via an HTTP/HTTPS proxy firewall. Direct internet connections are filtered, so I developed a hack to make it to work via an HTTP(S) proxied connection.

Basically I ripped the code from this ASPN snippet, and derived the urllib proxied transport to create a new one.

To use it behind a proxy, you can copy the libgmail example, and define the proxy this way:

import libgmail

libgmail.PROXY_URL = 'www.myproxy.org:3128'  # Define the proxy.

ga = libgmail.GmailAccount("google@gmail.com", "mymailismypass")
ga.login()
folder = ga.getMessagesByFolder('inbox')

for thread in folder:
    print thread.id, len(thread), thread.subject
    for msg in thread:
        print "  ", msg.id, msg.number, msg.subject
    print msg.source

I patched the libgmail.py file and write the proxied transport in another one: gmail_transport.py. You will need to copy both the file gmail_transport.py and the modified version of libgmail.py (overwrite it) into the same directory libgmail.py is installed.

There is also a diff file available, for libgmail-0.6.1.2.

Update (2007-10-06): It’s now possible to use proxy authentication. To do it so, you must use the proxy host address on the previous example this way: user:password@myproxy.org:3128 where user and password are your proxy user and password respectively. You’ll have to download again the file gmail_transport.py.

Now, the example to use it behind a proxy requiring with user/passwd authentication is like this:

import libgmail

# Connect from behind a proxy www.myproxy.org:3128 using
# proxy authentication user = 'john', password = 'proxy4321'
libgmail.PROXY_URL = 'john:proxy4321@www.myproxy.org:3128'  # Define the proxy 

ga = libgmail.GmailAccount("google@gmail.com", "mymailismypass")
ga.login()
folder = ga.getMessagesByFolder('inbox')

for thread in folder:
    print thread.id, len(thread), thread.subject
    for msg in thread:
        print "  ", msg.id, msg.number, msg.subject
    print msg.source

Posted by Boriel as Computer Science, Programming at 7.57 pm

Rate This Post: 1 Stars2 Stars3 Stars4 Stars5 Stars
2 Votes | Average: 5 out of 52 Votes | Average: 5 out of 52 Votes | Average: 5 out of 52 Votes | Average: 5 out of 52 Votes | Average: 5 out of 5 (2 votes, average: 5 out of 5)
9 Comments »