使用`requests`向twitter发送请求:OpenSSL.SSL.SysCallError:(104,'由peer重置连接')

时间:2015-04-10 02:21:41

标签: python python-requests pyopenssl

我正在使用python包requestshttps://mobile.twitter.com/username/following发送请求。

首先,我遇到异常:requests.exceptions.SSLError:[Errno 8] _ssl.c:504:EOF违反协议。为了解决这个问题,我按照这个解决方案写了一个SSLAdapter来指定PROTOCOL_TLSv1。

之后,我遇到另一个异常:requests.exceptions.SSLError:[Errno bad handshake](-1,'Unexpected EOF')。而且,我发现了这一点,但我发送请求并在同一过程中接收数据。

然后,我使用requestshttps://api.twitter.com/1.1/friends/ids.json发送请求。第二个例外消失了(仍然不明白为什么)。但我遇到第三个异常:OpenSSL.SSL.SysCallError:(104,'由peer重置连接')。我在SO中找到了this。我在发送请求之前添加time.sleep(10)。但第三个例外仍然存在。

所以第二个和第三个例外仍然发生。也许响应内容太大了?或者这是Twitter服务器的问题(有些SO solutions说的)。

https://mobile.twitter.com/username/followers发送请求的代码位于here

并且代码向twitter api followers/ids发送请求的请求位于here

这是第二个例外的追溯:

    Traceback (most recent call last):
      File "gen_friendship.py", line 218, in <module>
        start(sed_login, max_depth)
      File "gen_friendship.py", line 201, in start
        get_followers(node)
      File "gen_friendship.py", line 131, in get_followers
        response = retrieve(FOLLOWERS_URL % name)
      File "gen_friendship.py", line 33, in retrieve
        response = requester.get(url)
      File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 476, in get
        return self.request('GET', url, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 464, in request
        resp = self.send(prep, **send_kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 576, in send
        r = adapter.send(request, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 431, in send
        raise SSLError(e, request=request)
    requests.exceptions.SSLError: [Errno bad handshake] (-1, 'Unexpected EOF')

这是第三个例外的追溯:

    Traceback (most recent call last):
      File "gen_friendship_with_i.py", line 283, in <module>
        start(sed_login, max_depth)
      File "gen_friendship_with_i.py", line 266, in start
        get_followers(node)
      File "gen_friendship_with_i.py", line 150, in get_followers
        response = retrieve(FOLLOWERS_URL, params)
      File "gen_friendship_with_i.py", line 72, in retrieve
        response = requester.get(url, params = params, headers = headers)
      File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 476, in get
        return self.request('GET', url, **kwargs)
      File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 464, in request
        resp = self.send(prep, **send_kwargs)
      File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 576, in send
        r = adapter.send(request, **kwargs)
      File "/usr/lib/python2.6/site-packages/requests/adapters.py", line 370, in send
        timeout=timeout
      File "/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
      File "/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py", line 374, in _make_request
        httplib_response = conn.getresponse()
      File "/usr/lib64/python2.6/httplib.py", line 990, in getresponse
response.begin()
      File "/usr/lib64/python2.6/httplib.py", line 391, in begin
version, status, reason = self._read_status()
      File "/usr/lib64/python2.6/httplib.py", line 349, in _read_status
        line = self.fp.readline()
      File "/usr/lib64/python2.6/socket.py", line 433, in readline
        data = recv(1)
      File "/usr/lib/python2.6/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 188, in recv
        data = self.connection.recv(*args, **kwargs)
    OpenSSL.SSL.SysCallError: (104, 'Connection reset by peer')

0 个答案:

没有答案