如何在tweepy中设置socks5代理?

时间:2017-11-21 00:07:22

标签: proxy tweepy

要ssh登录我的vps,代码可以成功运行。

import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
for info in tweets:
    print(info.text)

设置与代理相关的所有软件,并测试socks代理。

url="https://www.youtube.com/watch?v=l9AC98amjSA"
youtube-dl --proxy socks5://127.0.0.1:1080 $url -o /tmp/downName

经验证,youtube上的视频可以使用socks5代理下载 所有与代理相关的软件都处于良好状态 现在我想在我的本地电脑中设置socks5代理,根据上面的代码只改变了一行。

api = tweepy.API(auth,proxy="127.0.0.1:1080")

错误信息如下。

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 588, in urlopen
    self._prepare_proxy(conn)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 803, in _prepare_proxy
    conn.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 291, in connect
    self._tunnel()
  File "/usr/lib/python3.5/http/client.py", line 827, in _tunnel
    (version, code, message) = response._read_status()
  File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.5/socket.py", line 576, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

添加socks代理语句也没有用。

import urllib.request
proxy_support = urllib.request.ProxyHandler({'sock5': 'localhost:1080'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)

import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth) # or api = tweepy.API(auth,proxy="127.0.0.1:1080")
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)
for info in tweets:
    print(info.text)

如何在tweety中设置socks5代理?
根据Tarun Lalwani的说法,安装包和代码发生了变化,仍然无法正常工作。

 sudo pip3   install -U requests[socks]

设置所有相关的代理软件。

sudo netstat -anlp |grep 1080
tcp        0      0 127.0.0.1:1080          0.0.0.0:*               LISTEN      1465/python3        
udp        0      0 127.0.0.1:1080          0.0.0.0:*                           1465/python3  

然后进入python3控制台。

import tweepy
consumer_key="xxx"
consumer_secret="xxx"
access_token="xxx"
access_token_secret="xxx"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth,proxy="socks5://127.0.0.1:1080")
tweets = api.user_timeline(screen_name="realDonaldTrump",weet_mode="extended",count=200)

发生其他错误信息。

  
    
      

tweets = api.user_timeline(screen_name =“realDonaldTrump”,weet_mode =“extended”,count = 200)

    
  
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/urllib3/contrib/pyopenssl.py", line 441, in wrap_socket
    cnx.do_handshake()
  File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1716, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1431, in _raise_ssl_error
    raise WantReadError()
OpenSSL.SSL.WantReadError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line 326, in connect
    ssl_context=context)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/util/ssl_.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/contrib/pyopenssl.py", line 445, in wrap_socket
    raise timeout('select timed out')
socket.timeout: select timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.5/dist-packages/urllib3/util/retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/packages/six.py", line 686, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 349, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 309, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 187, in execute
    proxies=self.api.proxy)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 521, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 245, in _call
    return method.execute()
  File "/usr/local/lib/python3.5/dist-packages/tweepy/binder.py", line 189, in execute
    raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: SOCKSHTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)

1 个答案:

答案 0 :(得分:1)

两件事。一个你需要requests[socks]

pip install requests[socks]

接下来,您需要使用socks格式的代理

api = tweepy.API(auth,proxy="socks5://127.0.0.1:1080")