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@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 <==
On 17 February 2011 10:28, BJ Swope wrote:
> Imap is not on port 443. IIRC, it's late and I'm to lazy to even google it
> right now, but it's port 143 isn't it.
I have tried that, and I get the following with imaplib:
Traceback (most recent call last):
File "D:\MyProjects\gmail.py", line 17, in <module>
m = imaplib.IMAP4_SSL('imap.gmail.com', 143)
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 1149, in open
self.sock = socket.create_connection((host, port))
File "C:\Python26\lib\socket.py", line 514, in create_connection
raise error, msg
socket.error: [Errno 10061] No connection could be made because the
target machine actively refused it
And this one with libgmail:
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 143, 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 [Errno 10060] A connection attempt
failed because the connected party did not properly respond after a
period of time, or establishe
d connection failed because connected host has failed to respond>
:-( :-(
Thank you for your answer.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>
--
What type of result do you get trying port 993 ?
Malcolm
i.e. The port Google explicitly says to use for IMAP in Gmail's help docs.
Cheers,
Chris
On 17 February 2011 11:44, Malcolm Greene wrote:
> Andrea,
>
> What type of result do you get trying port 993 ?
Thank you for your answer. I have tried that, with imaplib and
libgmail. This is what I get with imaplib:
Traceback (most recent call last):
File "D:\MyProjects\gmail.py", line 17, in <module>
m = imaplib.IMAP4_SSL('imap.gmail.com', 993)
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 1149, in open
self.sock = socket.create_connection((host, port))
File "C:\Python26\lib\socket.py", line 514, in create_connection
raise error, msg
socket.error: [Errno 10061] No connection could be made because the
target machine actively refused it
And this with libgmail:
Traceback (most recent call last):
File "D:\MyProjects\gmail2.py", line 10, in <module>
period of time, or established connection failed because connected
host has failed to respond>
This thing is driving me crazy, and I'm not even doing it for fun...
Thank you for your suggestions.
Have you tried temporarily turning off your Windows firewall software
and/or any locally installed internet security software like Norton,
Avast, etc? You don't need to turn off your entire security package,
just the component that controls internet access.
If the problem can not be resolved locally, then I believe you need to
contact your IT department to have them create the firewall rules that
will allow you to access gmail services. I'm basing that suggestion on
googling the various error messages (10060, 10061) you received and
reading the answers. That's my interpretation, anyway.
I'm a big fan of your wxPython and GUI builder contributions!!
Regards,
Malcolm
On 17 February 2011 19:20, <pyt...@bdurham.com> wrote:
> Hi Andrea,
>
> Have you tried temporarily turning off your Windows firewall software
> and/or any locally installed internet security software like Norton,
> Avast, etc? You don't need to turn off your entire security package,
> just the component that controls internet access.
I'd love to, but it's not under my control. The IT department has an
iron grip on these things and there is no way I can do what you
proposed :-(
>
> If the problem can not be resolved locally, then I believe you need to
> contact your IT department to have them create the firewall rules that
> will allow you to access gmail services. I'm basing that suggestion on
> googling the various error messages (10060, 10061) you received and
> reading the answers. That's my interpretation, anyway.
I though that I could do what Chrome/Firefox do: as far as I can
understand, they seem to use the company proxy settings to access the
web (and this is also what the "about:config" in Firefox is telling
me: it gives all the proxy servers and ports I need). However, even
the simplest piece of code using urllib2 and trying to read the Python
web page like this:
import urllib2
user_agent = 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.10)
Gecko/20100915\
Ubuntu/10.04 (lucid) Firefox/3.6.10'
request = urllib2.Request('http://www.python.org/')
request.add_header('User-agent', user_agent )
proxy_info = {
'user' : username,
'pass' : password,
'host' : proxy_server,
'port' : 993 # or 8080 or whatever
}
proxy = "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info
proxy_support = urllib2.ProxyHandler({"http" : proxy})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
urllib2.urlopen(request)
Fails with a variety of errors depending on which port I use:
- Port 80: urllib2.HTTPError: HTTP Error 407: Proxy Authentication
Required ( The ISA Server requires authorization to fulfill the
request. Access to the Web Proxy filter is denied. )
- Port 143: urllib2.URLError: <urlopen error [Errno 10060] A
connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed
because connected host has failed to respond>
- Port 8080: urllib2.HTTPError: HTTP Error 403: Forbidden ( The server
denied the specified Uniform Resource Locator (URL). Contact the
server administrator. )
Etc... etc...
I am going to try all the ports from 1 to 10000 and see what happens,
just for the fun of it. I am sure I will get 10000 errors, with mixed
frequency. :-( :-(
If someone can see what I am doing wrong, please do let me know... I'm
running our of ideas...
>
> I'm a big fan of your wxPython and GUI builder contributions!!
Thank you, I'm happy you found them useful!
On Fri, Feb 18, 2011 at 09:40:19AM +0500, Andrea
Gavana wrote:
> Fails with a variety of errors depending on which port I use:
>
> - Port 80: urllib2.HTTPError: HTTP Error 407: Proxy Authentication
> Required ( The ISA Server requires authorization to fulfill the
> request. Access to the Web Proxy filter is denied. )
Wow, that's much better! So your python proxy is
working but just doesn't know how to authenticate.
Unfortunately I don't remember how to specify
proxy auth in python, but this is definitely
achievable.
Please 1st make ports 80 and 443 working, then you
can try IMAP and other ports.
In general, you're out of luck if your corporate
proxy only supports connecting to ports 80 and 443
in outside world.
But if you own a box in the wild, you can setup
ssh/openvpn/socks server/whatever on port 443 on
that box and use it as a kind of access broker to
the rest of ports in the world.
--
With best regards,
xrgtn