限制SSL连接的带宽

时间:2015-01-30 15:26:16

标签: python sockets ssl python-requests throttling

asked a question关于如何限制python上传,它将我发送到this answer,在那里我被告知了一个名为socket-throttle的小帮助库。对于常规HTTP而言,这一切都很好,而且对于套接字的大多数普通用途也可能。但是,我尝试限制SSL连接,并尝试将socket-throttle与库存SSL库(由requests隐式使用)组合在一起导致库中的异常深处:< / p>

  File "***.py", line 590, in request
    r = self.session.get(url, headers=extra_headers)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 394, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 382, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 485, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 324, in send
    timeout=timeout
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 478, in urlopen
    body=body, headers=headers)
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 285, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python2.7/httplib.py", line 973, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1007, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 969, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 829, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 791, in send
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connection.py", line 95, in connect
    ssl_version=resolved_ssl_version)
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util.py", line 643, in ssl_wrap_socket
    ssl_version=ssl_version)
  File "/usr/lib/python2.7/ssl.py", line 487, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 211, in __init__
    socket.__init__(self, _sock=sock._sock)
  File "***/socket_throttle.py", line 54, in __getattr__
    return getattr(self._wrappedsock, attr)
AttributeError: '_socket.socket' object has no attribute '_sock'
嗯,这是一个沮丧。正如您所知,ssl包正在尝试使用套接字的私有字段_sock而不是socket本身。 (不是你不应该从外面访问它们的私有字段吗?Grr。)如果我试图将自己注入我ThrottledSocket对象的那个字段,我会遇到这个问题:

    File "/home/alex/dev/jottalib/src/jottalib/JFS.py", line 590, in request
    r = self.session.get(url, headers=extra_headers)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 394, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 382, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 485, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 324, in send
    timeout=timeout
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 478, in urlopen
    body=body, headers=headers)
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 285, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python2.7/httplib.py", line 973, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1007, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 969, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 829, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 791, in send
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connection.py", line 95, in connect
    ssl_version=resolved_ssl_version)
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util.py", line 643, in ssl_wrap_socket
    ssl_version=ssl_version)
  File "/usr/lib/python2.7/ssl.py", line 487, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 241, in __init__
    ciphers)
TypeError: must be _socket.socket, not ThrottledSocket

现在怎样?还有其他地方我可以对python通信进行速率限制吗?或者是否有更简洁的方法来覆盖套接字实现?无论如何,这都是没有意义的,因为ssl包只是试图完全绕过它。

2 个答案:

答案 0 :(得分:4)

根据您的要求,您可以而且可能应该在操作系统级别而不是在应用程序级别上解决此特定问题。

在操作系统级别上接近这一点有两个好处。首先,如何使用所涉及的套接字(HTTP或HTTPS或IRC或死亡数据包的一些ping - 无关紧要)并没有什么区别。其次,你越多地分离系统的不同组件,就越容易在事后进行更改并调试问题。

有一些工具(至少对于符合POSIX的系统)用于限制网络接口和/或进程的带宽。您可能希望查看这些内容,例如:

  • trickle(用于整理流程的流量)
  • wondershaper(为了塑造整个网络接口的流量,我实际上已经在现代的Ubuntu中使用了它,并且它工作得很好)

这些讨论可能对您有用:

答案 1 :(得分:1)

看起来你正试图限制HTTP请求。如果是这种情况,您可以尝试使用RequestsThrottler。 Python requests也比httplib更好。