IMAP4_SSL, libgmail, GMail and corporate firewall/proxy

Andrea Gavana andrea.gavana at gmail.com
Wed Feb 16 23:58:59 EST 2011


Hi All,

    I apologize in advance if I'm going to write very stupid things,
my expertise in http/socket/imap stuff is very close to zero. I'm
using Python 2.6.5 on Windows XP SP3.

I am trying to access my GMail account from my office, and it appears
our company's firewall is blocking all SMTP/POP3/IMAP attempts or
there is simply something I don't understand. My first try was to use
imaplib as follows:


import imaplib

m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
m.login(username, password)


And I get the following:


Traceback (most recent call last):
  File "D:\MyProjects\gmail.py", line 17, in <module>
    m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
  File "C:\Python26\lib\imaplib.py", line 1138, in __init__
    IMAP4.__init__(self, host, port)
  File "C:\Python26\lib\imaplib.py", line 163, in __init__
    self.open(host, port)
  File "C:\Python26\lib\imaplib.py", line 1150, in open
    self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
  File "C:\Python26\lib\ssl.py", line 350, in wrap_socket
    suppress_ragged_eofs=suppress_ragged_eofs)
  File "C:\Python26\lib\ssl.py", line 118, in __init__
    self.do_handshake()
  File "C:\Python26\lib\ssl.py", line 293, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [Errno 8] _ssl.c:480: EOF occurred in violation of protocol


OK. Then I googled back and forth for a possible solution, and I got a
hint that (maybe) the problem could be related to proxy
authentication. And I tried this solution (which uses SocksiPy) I
found on the web (username2 and password2 are the user name and
password for our company's proxy):

import socks
import socket

proxy_ip = "10.100.100.20"   # Your proxy IP/DNS here

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_ip, 8080, True,
username2, password2)
socket.socket = socks.socksocket

import imaplib

m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
m.login(username, password)


This solution just hangs forever at the line:

m = imaplib.IMAP4_SSL('imap.gmail.com', 443)

And it never returns (it never gets to the m.login() stuff).


Then I tried with libgmail, giving it the option of setting the proxy
name and port:

import libgmail

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

ga = libgmail.GmailAccount(username, password)
ga.login()


And I got this at first:

Traceback (most recent call last):
  File "D:\MyProjects\gmail2.py", line 8, in <module>
    ga.login()
  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
    pageData = self._retrievePage(req)
  File "C:\Python26\lib\site-packages\libgmail.py", line 340, in _retrievePage
    req = ClientCookie.Request(urlOrRequest)
  File "C:\Python26\lib\site-packages\mechanize\_request.py", line 31,
in __init__
    if not _rfc3986.is_clean_uri(url):
  File "C:\Python26\lib\site-packages\mechanize\_rfc3986.py", line 63,
in is_clean_uri
    return not bool(BAD_URI_CHARS_RE.search(uri))
TypeError: expected string or buffer


Then I brutally hacked into mechanize here and there and I was able to
fix all non-internet related errors. And when I try the libgmail
solution above now I get:

Traceback (most recent call last):
  File "D:\MyProjects\gmail2.py", line 8, in <module>
    ga.login()
  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
    pageData = self._retrievePage(req)
  File "C:\Python26\lib\site-packages\libgmail.py", line 348, in _retrievePage
    resp = self.opener.open(req)
  File "C:\Python26\lib\site-packages\mechanize\_opener.py", line 193, in open
    response = urlopen(self, req, data)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 344, in _open
    '_open', req)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 332, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 1171, in https_open
    return self.do_open(conn_factory, req)
  File "C:\Python26\lib\site-packages\gmail_transport.py", line 145, in do_open
    return ClientCookie.HTTPSHandler.do_open(self,
ProxyHTTPSConnection.new_auth(self.proxy, self.proxy_user,
self.proxy_passwd), req)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 1118, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error Tunnel connection failed: 403
Forbidden ( The ISA Server denied the specified Uniform Resource
Locator (URL).  )>


Just in case, I have tried also with port 80, with similar results:

Traceback (most recent call last):
  File "D:\MyProjects\gmail2.py", line 8, in <module>
    ga.login()
  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
    pageData = self._retrievePage(req)
  File "C:\Python26\lib\site-packages\libgmail.py", line 348, in _retrievePage
    resp = self.opener.open(req)
  File "C:\Python26\lib\site-packages\mechanize\_opener.py", line 193, in open
    response = urlopen(self, req, data)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 344, in _open
    '_open', req)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 332, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 1171, in https_open
    return self.do_open(conn_factory, req)
  File "C:\Python26\lib\site-packages\gmail_transport.py", line 145, in do_open
    return ClientCookie.HTTPSHandler.do_open(self,
ProxyHTTPSConnection.new_auth(self.proxy, self.proxy_user,
self.proxy_passwd), req)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 1118, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error Tunnel connection failed: 407 Proxy
Authentication Required ( The ISA Server requires authorization to
fulfill the request. Access to the Web Proxy filter is denied.  )>


What am I doing wrong? Is there anything I should do differently? Or
anything else I should try?

Thank you for your help, any suggestion is highly appreciated.


Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

==> Never *EVER* use RemovalGroup for your house removal. You'll
regret it forever.
http://thedoomedcity.blogspot.com/2010/03/removal-group-nightmare.html <==



More information about the Python-list mailing list