0

macOS 10.12.3 python 2.7.13 requests 2.13.0
我使用请求包发送发布请求。此请求需要在发布数据之前登录。所以我使用 request.Session() 并加载登录的 cookie。然后我使用这个会话以循环模式发送帖子数据。
我曾经在 Windows 和 Linux 中运行此代码并没有错误。
简单代码:

s = request.Session()
s.cookies = cookieslib.LWPCookieJar('cookise')
s.cookies.load(ignore_discard=True)
for user_id in range(100,200):
    url = 'http://xxxx'
    data = { 'user': user_id, 'content': '123'}
    r = s.post(url, data)
    ...

但是程序经常(大约每个间隔)崩溃,错误是AttributeError: 'module' object has no attribute 'kqueue'

Traceback (most recent call last):
  File "/Users/gasxia/Dev/Projects/TgbookSpider/kfz_send_msg.py", line 90, in send_msg
    r = requests.post(url, data)  # catch error if user isn't exist
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 535, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 588, in urlopen
    conn = self._get_conn(timeout=pool_timeout)
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 241, in _get_conn
    if conn and is_connection_dropped(conn):
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/util/connection.py", line 27, in is_connection_dropped
    return bool(wait_for_read(sock, timeout=0.0))
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/util/wait.py", line 33, in wait_for_read
    return _wait_for_io_events(socks, EVENT_READ, timeout)
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/util/wait.py", line 22, in _wait_for_io_events
    with DefaultSelector() as selector:
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/util/selectors.py", line 431, in __init__
    self._kqueue = select.kqueue()
AttributeError: 'module' object has no attribute 'kqueue'
4

1 回答 1

1

如果您使用类似eventletor的东西,这看起来像是一个通常会出现的问题gevent,两者都对select模块进行了猴子补丁。如果您使用它们来实现异步,则需要确保在导入请求之前应用了这些猴子补丁。这是一个已知的错误,在这个问题中被跟踪。

于 2017-02-08T19:42:15.093 回答