gleitz / howdoi

instant coding answers via the command line
http://blog.gleitzman.com/post/43330157197/howdoi-instant-coding-answers-via-the-command-line
MIT License
10.6k stars 870 forks source link

"Failed to establish network connection", behind proxy #45

Closed eddie-dunn closed 11 years ago

eddie-dunn commented 11 years ago

At home I can use howdoi, no problem. At work I get this: edwardd@lnxedwardd> howdoi say hello in german Failed to establish network connection edwardd@lnxedwardd>

I edited howdoi.py to print the ConnectionError before returning, which displays this message: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

No idea what it means, but I'm guessing it is a proxy error, mainly because all network errors here at work usually have to do with the proxy.

Is it possible to make howdoi work behind a proxy?

gleitz commented 11 years ago

Thanks for the bug report. Please try adding the following proxyDict in the get_result function of howdoi.py

$ howdoi requests proxy
http_proxy  = "10.10.1.10:3128"
https_proxy = "10.10.1.11:1080"
ftp_proxy   = "10.10.1.10:3128"
proxyDict = { 
              "http"  : http_proxy, 
              "https" : https_proxy, 
              "ftp"   : ftp_proxy
            }
r = requests.get(url, headers=headers, proxies=proxyDict)

I'm going to close this issue. Please let me know if this solution doesn't work.

eddie-dunn commented 11 years ago

OK, after some more delving I realized that it's the "https" in in SEARCH_URL that made the script fail. Changing it to "http" solved everything (my proxies are already set system wide through environment variables, meaning howdoi uses them automatically).

Here's what I found about the problem: http://stackoverflow.com/questions/11774612/python-requests-can-not-send-https-request http://stackoverflow.com/questions/12343064/python-rauth-requests-openssl-cannot-get-api-twitter

The problem seems to be because of certificate verification errors, as I can do https requests with urllib2 -- and urllib2 doesn't do any certificate verification.

I realize my problem might be quite the corner-case, but would you accept a patch that modifies howdoi to use http as fallback-mode? I'd like to have it work out of the box :)

eddie-dunn commented 11 years ago

OK, seems this is a known issue with the requests library: https://github.com/kennethreitz/requests/issues/1204

sigmavirus24 commented 11 years ago

@gleitz our proxy support may be changing (as of the next release). We might be requiring 'http://' and 'https://' for the URLs, e.g., 'http://10.10.1.10:3128'. Just wanted to give you a heads-up.

gleitz commented 11 years ago

Good sleuthing, @eddie-dunn! Would it be correct to assume that if I wait for the new requests release this issue will be fixed without any code changes to howdoi?

eddie-dunn commented 11 years ago

Well, it looks like it may be a while before the issue is solved. Regardless, I think a fallback http-mode for howdoi is still a good idea.

eddie-dunn commented 11 years ago

Well, it's been 5 months and nothing seems to have improved. Would you accept a pull request implementing an HTTP fallback mode?

gleitz commented 11 years ago

If the http-mode fallback works and does not negatively effect other users I would be happy to accept it.

eddie-dunn commented 11 years ago

Cool. Pull request here: https://github.com/gleitz/howdoi/pull/60