【问题标题】:Why this code only returns its output only once?为什么这段代码只返回一次它的输出?
【发布时间】:2016-01-30 18:03:30
【问题描述】:

我在中国。我使用代理连接到互联网,当我想代理程序时,我使用代理链隧道。现在,问题是:我有这段代码,这是针对 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,但一切都保持不变。有人知道吗?

【问题讨论】:

  • 我已经安装了一个与远程机器相同的 python 设置,同样的情况也会发生。它只输出一次。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2013-08-24
    • 2022-11-13
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2023-02-09
    相关资源
    最近更新 更多