为什么此代码只返回一次输出



我在中国。我使用代理连接到互联网,当我想代理程序时,我会用代理链对其进行隧道传输。现在,问题:我有这段代码,这是针对Youtube API的简单身份验证:

import httplib2
import os
import logging
from oauth2client import tools
from oauth2client.client import AccessTokenCredentials
#from oauth2client.client import AccessTokenRefreshError
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from googleapiclient.errors import HttpError
import urllib
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def authenticate():
    httplib2.debuglevel = 4
    acc_token = "ya29.dgLFP1i6jTuc-hnaC9D704i2jbQ2HOHweSqxjL9GxSFBg8QgvU"
    user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
    flow = AccessTokenCredentials(acc_token, user_agent)
    http = flow.authorize(httplib2.Http())
    service = build('youtube', 'v3', http=http)
    return(service)

def initialize_upload(youtube):
    tags = 'classical music', 'yehudi mehunin'
    body = dict(
        snippet=dict(
            title='some title',
            description='a  description',
            tags=tags,
            categoryId='4'
        ),
        status=dict(
            privacyStatus='Private'
        )
    )
    youtube.videos().insert(part=",".join(body.keys()), body=body, media_body=MediaFileUpload(
        '1977.mp4', mimetype='video/mp4', chunksize=1024 * 1024, resumable=False))

def first():
    youtube = authenticate()
    initialize_upload(youtube)
first()

当我第一次打开我的电脑时,我激活了我的 virtualenv,在不代理的情况下从终端执行脚本,然后我得到了超时(我必须手动打破它才能退出),我得到这个输出:

^CTraceback (most recent call last):
  File "youtubeconnect.py", line 48, in <module>
    first()
  File "youtubeconnect.py", line 45, in first
    youtube = authenticate()
  File "youtubeconnect.py", line 21, in authenticate
    service = build('youtube', 'v3', http=http)
  File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/oauth2client-1.5.2-py3.5.egg/oauth2client/util.py", line 140, in positional_wrapper
  File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/google_api_python_client-1.4.2-py3.5.egg/googleapiclient/discovery.py", line 196, in build
  File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/google_api_python_client-1.4.2-py3.5.egg/googleapiclient/discovery.py", line 242, in _retrieve_discovery_doc
  File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/oauth2client-1.5.2-py3.5.egg/oauth2client/client.py", line 596, in new_request
  File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/httplib2-0.9.2-py3.5.egg/httplib2/__init__.py", line 1314, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/httplib2-0.9.2-py3.5.egg/httplib2/__init__.py", line 1064, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/home/xavier/Code/autotube/venv/lib/python3.5/site-packages/httplib2-0.9.2-py3.5.egg/httplib2/__init__.py", line 987, in _conn_request
    conn.connect()
  File "/usr/lib/python3.5/http/client.py", line 1229, in connect
    super().connect()
  File "/usr/lib/python3.5/http/client.py", line 826, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.5/socket.py", line 702, in create_connection
    sock.connect(sa)
KeyboardInterrupt

现在,我第一次使用代理链隧道运行它,并得到响应:

|DNS-request| www.googleapis.com 
|S-chain|-<>-127.0.0.1:1080-<><>-4.2.2.2:53-<><>-OK
|DNS-response| www.googleapis.com is 74.125.29.95
|S-chain|-<>-127.0.0.1:1080-<><>-74.125.29.95:443-<><>-OK
send: b'GET /discovery/v1/apis/youtube/v3/rest HTTP/1.1rnHost: www.googleapis.comrnuser-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36rnauthorization: Bearer ya29.dgLFP1i6jTuc-hnaC9D704i2jbQ2HOHweSqxjL9GxSFrnaccept-encoding: gzip, deflaternrn'
reply: 'HTTP/1.1 200 OKrn'
header: Expires header: Date header: ETag header: Vary header: Vary header: Content-Type header: Content-Encoding header: X-Content-Type-Options header: X-Frame-Options header: X-XSS-Protection header: Server header: Content-Length header: Age header: Cache-Control header: Alternate-Protocol header: Alt-Svc (venv) xavier@xavier:~/Code/autotube$ proxychains python3 youtubeconnect.py 
ProxyChains-3.1 (http://proxychains.sf.net)

现在,为什么当我再次运行它时,无论是隧道还是不隧道,脚本都会执行并且不再提供任何输出???脚本执行没有错误,仅此而已。无输出。我只能在重新启动计算机时获得输出。API 或某些库是否使用缓存或类似的东西?我也尝试停用和重新激活 venv,但一切都保持不变。有人知道吗?

好的,这与 httplib2 如何处理缓存有关。它启用了自动缓存,因此为了获得响应的副本,您必须在 httplib2 中指定一个缓存目录。Http('.cache') 对象。如果不专门修改缓存标头以忽略缓存并绕过它再次将请求发送到服务器,则无法避免自动缓存。请记住,如果您正在代理,您的代理也会有一个缓存。有关HTTP,httplib2和Python3的良好信息,请查看免费资源:http://www.diveintopython3.net/http-web-services.html

相关内容

  • 没有找到相关文章