Forums

Twitter APIs [Errno 111] Connection refused

I'm building an app that connects to the twitter APIs, the code runs well on localhost, but on pythonanywhere it gives me this:

Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/iahvector/twitterWebApp/TwitterWebApp/twitterApp/views.py" in index
26.         resp, content = client.request(settings.REQUEST_TOKEN_URL, "GET")
File "/usr/local/lib/python2.7/site-packages/oauth2/__init__.py" in request
682.             connection_type=connection_type)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in request
1597.                     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in _request
1345.         (response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in _conn_request
1281.                   conn.connect()
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in connect
1052.           raise socket.error, msg

Exception Type: error at /
Exception Value: [Errno 111] Connection refused

When I checked error.log, nothing appears there.

It looks like oauth2 needs you to specifically specify a proxy. If you replace this

client.request(settings.REQUEST_TOKEN_URL, "GET")

with

client.request(settings.REQUEST_TOKEN_URL, "GET", 'proxy.server')

it may work.

Unfortunately, It didn't work:

Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/iahvector/twitterWebApp/TwitterWebApp/twitterApp/views.py" in index
26.         resp, content = client.request(settings.REQUEST_TOKEN_URL, "GET", 'proxy.server')
File "/usr/local/lib/python2.7/site-packages/oauth2/__init__.py" in request
682.             connection_type=connection_type)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in request
1597.                     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in _request
1345.         (response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in _conn_request
1281.                   conn.connect()
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in connect
1052.           raise socket.error, msg

Exception Type: error at /
Exception Value: [Errno 111] Connection refused

Still nothing in the error logs.

Hm, I think you actually have to pass in a parameter called proxy_info=... httplib2 is then expecting an instance of something from the socks module. Have a look at this:

https://code.google.com/p/httplib2/wiki/Examples

According to the example in the link, I should use this?

resp, content = client.request(settings.REQUEST_TOKEN_URL, "GET", proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, "proxy.server", 8000))

Which port should I use?

3128

The oauth2 library installed on the server doesn't support proxies:

Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/iahvector/twitterWebApp/TwitterWebApp/twitterApp/views.py" in index
28.         resp, content = client.request(settings.REQUEST_TOKEN_URL, "GET", proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, "proxy.server", 3128))

Exception Type: TypeError at /

From your web app's error page, it looks like you're picking up a version of oauth2 that's installed in your sandbox, so you can uninstall that and then install the branch that supports proxies:

pip uninstall oauth2
pip install --user https://github.com/earth2marsh/python-oauth/archive/master.zip

I installed the referred library and tried using it as in the read me example

PROXY_DOMAIN = "proxy.server:3128"
resp, content = client.request(settings.REQUEST_TOKEN_URL, "GET", PROXY_DOMAIN)

and it gave me

Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/iahvector/twitterWebApp/TwitterWebApp/twitterApp/views.py" in index
32.         resp, content = client.request(settings.REQUEST_TOKEN_URL, "GET", str(PROXY_DOMAIN))
File "/home/iahvector/.local/lib/python2.7/site-packages/oauth2/__init__.py" in request
574.             uri = uri.replace(urlparse.urlparse(uri),proxy,1)

Exception Type: TypeError at /
Exception Value: expected a character buffer object

I found this example that explains how to use the original oauth2 library with proxy. I tried it with the globally installed oauth2 and the one you suggested but both gave me a 403 forbidden error.

proxy = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'proxy.server', 3128)
client = oauth.Client(consumer, proxy_info=proxy)
resp, content = client.request(settings.REQUEST_TOKEN_URL, "GET")

The traceback:

Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/iahvector/twitterWebApp/TwitterWebApp/twitterApp/views.py" in index
32.         resp, content = client.request(settings.REQUEST_TOKEN_URL, "GET")
File "/usr/local/lib/python2.7/site-packages/oauth2/__init__.py" in request
682.             connection_type=connection_type)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in request
1597.                     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in _request
1345.         (response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in _conn_request
1281.                   conn.connect()
File "/usr/local/lib/python2.7/site-packages/httplib2/__init__.py" in connect
894.                 self.sock.connect((self.host, self.port) + sa[2:])
File "/usr/local/lib/python2.7/site-packages/httplib2/socks.py" in connect
424.             self.__negotiatehttp(destpair[0], destpair[1])
File "/usr/local/lib/python2.7/site-packages/httplib2/socks.py" in __negotiatehttp
390.             raise HTTPError((statuscode, statusline[2]))

Exception Type: HTTPError at /
Exception Value: (403, 'Forbidden')

Oh dear, that's not a good sign. I've not seen this error with httplib2, but there we have had problems with urllib3 where it generates an error just like that when you try to connect using https through the Squid proxy. This tends to show up when people are trying to use Tweepy.

Now, I've had a good look around and the only httplib2 bug that seems to have been fixed back in 2011 (and the fix is definitely in the version we have installed). So I would still expect this to work for you -- but it might be the same problem. Do you know if the requests you're sending out are using https?

did anyone got through this error? I am also getting error 111

Hi there, the first thing to try would be to install everything into a virtualenv, to make sure you've got the absolute latest version of all the tweepy / httplib / urllib / oauth packages etc. There are some instructions here, in section 2

I had this same error. i solved this by giving "proxies" argument to the requests.request method.

request.request(url , proxies = {'http': 'http://proxy.server:3128', 'https': 'https://proxy.server:3128'} )