Forums

PRAW Connection refused

Hi,

I'm having trouble reaching reddit with PRAW. There are a couple other threads with similar problems SocketError when using PRAW and Did something change last night?.... I'm able to wget the same endpoint using the same user-agent.

Traceback (most recent call last):
  File "testpraw.py", line 7, in <module>
    for submission in subreddit.get_hot(limit=5):
  File "/home/mikenon/.local/lib/python2.7/site-packages/praw/__init__.py", line 434, in get_content
    page_data = self.request_json(url, params=params)
  File "/home/mikenon/.local/lib/python2.7/site-packages/praw/decorators.py", line 95, in wrapped
    return_value = function(reddit_session, *args, **kwargs)
  File "/home/mikenon/.local/lib/python2.7/site-packages/praw/__init__.py", line 469, in request_json
    response = self._request(url, params, data)
  File "/home/mikenon/.local/lib/python2.7/site-packages/praw/__init__.py", line 342, in _request
    response = handle_redirect()
  File "/home/mikenon/.local/lib/python2.7/site-packages/praw/__init__.py", line 315, in handle_redirect
    timeout=timeout, **kwargs)
  File "/home/mikenon/.local/lib/python2.7/site-packages/praw/handlers.py", line 135, in wrapped
    result = function(cls, **kwargs)
  File "/home/mikenon/.local/lib/python2.7/site-packages/praw/handlers.py", line 54, in wrapped
    return function(cls, **kwargs)
  File "/home/mikenon/.local/lib/python2.7/site-packages/praw/handlers.py", line 90, in request
    allow_redirects=False)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 460, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 246, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.reddit.com', port=80): Max retries exceeded with url: /r/pics/.json?limit=2 (Caused by <class 'socket.error'>: [Errno 111] Connection refused)`

The script is pretty basic:

#!/usr/bin/python
import praw
from pprint import pprint
reddit = praw.Reddit(user_agent='testing praw /u/mikenon')
subreddit = reddit.get_subreddit('pics')
for submission in subreddit.get_hot(limit=2):
    pprint(vars(submission))
    for comment in praw.helpers.flatten_tree(submission.comments):
        pprint(vars(comment))

Is it a proxy issue on PA's end? Or is there something I can fix?

Thank you

How does it behave when you're not using PRAW? The proxy server is working fine and showing a lot of hits against reddit. Try using wget or resuests and check the results. Perhaps there's an issue with PRAW.

Hi,

Yeah, wget and Requests work. I was under the impression that PRAW was working for people here, though? PRAW has a config option for setting a proxy to use, can/should I put PA's proxy there? Is it just http://proxy.server/ ?

Thanks

I'm thinking the proxy is the speed bump here. Here's what I've scraped up to do some debugging:

#!/usr/bin/python
import praw
import requests
import logging
import httplib
httplib.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

# Works
print 'Making basic Requests get'
requests.get('http://www.reddit.com/r/opensource/.json?limit=5')

# Doesn't work
print '\n\nImporting PRAW and requesting submissions'
r = praw.Reddit(user_agent='Trying out PRAW on PythonAnywhere',site_name='reddit_nossl')
submissions = r.get_subreddit('opensource').get_hot(limit=5)
print [str(x) for x in submissions]

Logs for requests.get(url) show

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): proxy.server

While requests from PRAW show

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): www.reddit.com

So it would seem there ought to be a way to coerce PRAW into using the proxy.

May be worth raising it as a bug against PRAW, or as @PSBCommentBot says, try and hack in a fix yourself...

I'm getting the same error. Does this have to do with a non-paid account? (limits, etc)

It's really a bug in PRAW -- it doesn't play nicely with the proxy we use for free accounts. So it will work fine with a paid account, which doesn't use a proxy.

Was something changed in the backend recently? I'm running a continuous script that was working earlier today, which is now getting this error (although I never restarted it between then and now).

Edit: It looks like PRAW is indeed using the proxy (whatever that means) but it's still giving an error. Traceback:

Traceback (most recent call last):
  File "sns.py", line 154, in <module>
    r.login(username, password)
  File "/home/sns/.local/lib/python2.7/site-packages/praw/__init__.py", line 906, in login
    self.request_json(self.config['login'], data=data)
  File "/home/sns/.local/lib/python2.7/site-packages/praw/decorators.py", line 223, in error_check
ed_function
    return_value = function(cls, *args, **kwargs)
  File "/home/sns/.local/lib/python2.7/site-packages/praw/__init__.py", line 407, in request_json
    response = self._request(url, params, data)
  File "/home/sns/.local/lib/python2.7/site-packages/praw/__init__.py", line 294, in _request
    timeout=timeout)
  File "/home/sns/.local/lib/python2.7/site-packages/praw/decorators.py", line 64, in __call__
    result = self.function(reddit_session, url, *args, **kwargs)
  File "/home/sns/.local/lib/python2.7/site-packages/praw/decorators.py", line 167, in __call__
    return self.function(*args, **kwargs)
  File "/home/sns/.local/lib/python2.7/site-packages/praw/helpers.py", line 137, in _request
    allow_redirects=False, auth=auth)
  File "/home/sns/.local/lib/python2.7/site-packages/requests/sessions.py", line 377, in post
    return self.request('POST', url, data=data, **kwargs)
  File "/home/sns/.local/lib/python2.7/site-packages/requests/sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/sns/.local/lib/python2.7/site-packages/requests/sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "/home/sns/.local/lib/python2.7/site-packages/requests/adapters.py", line 327, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='proxy.server', port=3128): Max retri
es exceeded with url: http://www.reddit.com/api/login/.json (Caused by <class 'socket.error'>: [Er
rno 111] Connection refused)

I've bounced the proxy server, just in case. But if it's still not working, it's probably because the PRAW library is simply incompatible with proxies -- there are several outstanding bugs in, eg, the rquests library and urllib3 which are know to cause problems with HTTPS and proxies.

For now the best solution is to upgrade. If you switch to a paid account and can't get it to work, we'll happily give you a full refund...

Everything seems to work now, more smoothly than before even. Thanks!

Now I know, as a free user I'm not really "entitled" to anything, but it does seem a little strange to break previously-working functions of the free accounts and then recommend upgrading as a solution, just saying...

Anyone interested in this should track the issue on praw's github...