Stack Trace for hnsh.py on proxy
hnsh.py is a console application to browse Hacker News articles, but it didn't work behind a proxy server. Here's the error.
By Ryan McGreal.
427 words. Approximately a 1 to 2 minute read.
Posted December 07, 2009 in Projects.
(Last Updated January 07, 2010)
Contents
1 | The Stack Trace | ||
2 | Update 1: Proxies With Urllib2 | ||
3 | Update 2: If No Proxy | ||
4 | Update 3: hnsh.py on GitHub |
1 The Stack Trace ↑
Here's the original stack trace (Python 2.6 on a Windows XP machine behind a proxy server) for Hacker News Shell.
C:\Python26>python hnsh.py
Traceback (most recent call last):
File "hnsh.py", line 558, in
hnsh = HackerNewsShell()
File "hnsh.py", line 314, in __init__
self.stories = self.h.getLatestStories(self.alreadyReadList)
File "hnsh.py", line 194, in getLatestStories
source = self.getSource("http://news.ycombinator.com")
File "hnsh.py", line 35, in getSource
f = urllib2.urlopen(url)
File "C:\Python26\lib\urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python26\lib\urllib2.py", line 389, in open
response = self._open(req, data)
File "C:\Python26\lib\urllib2.py", line 407, in _open
'_open', req)
File "C:\Python26\lib\urllib2.py", line 367, in _call_chain
result = func(*args)
File "C:\Python26\lib\urllib2.py", line 1140, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python26\lib\urllib2.py", line 1115, in do_open
raise URLError(err)
urllib2.URLError:
2 Update 1: Proxies With Urllib2 ↑
After prompting the user for their proxy server, you'll probably want to do something like this:
# Example: proxies is a dict
proxies = { 'http': 'http://proxy.domain.com:80' }
proxy_support = urllib2.ProxyHandler(proxies)
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
urllib2.urlopen(url)
This is untested, but it should work more or less as written.
3 Update 2: If No Proxy ↑
It may be helpful to note further that you can submit an empty proxies
dict in the above code if the internet connection is direct:
proxies = {}
This way, you can use the same code to connect to the URL whether or not the user is behind a proxy server.
4 Update 3: hnsh.py on GitHub ↑
Scott Jackson, author of hnsh.py, moved the code into git and posted the repository on GitHub:
Check this out from the source code:
Special thanks to Ryan McGreal (http://github.com/quandyfactory) for the code that makes hnsh work from behind a proxy.
flushes with pride