为什么这段代码只返回一次输出?

时间:2016-01-29 13:00:39

标签: python-3.x proxy youtube-api

我在中国。我使用代理连接到互联网,当我想代理程序时,我使用代理链进行隧道传输。现在,问题是:我有这个代码,对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.1\r\nHost: www.googleapis.com\r\nuser-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36\r\nauthorization: Bearer ya29.dgLFP1i6jTuc-hnaC9D704i2jbQ2HOHweSqxjL9GxSF\r\naccept-encoding: gzip, deflate\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
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,但一切都保持不变。有人知道吗?

1 个答案:

答案 0 :(得分:0)

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