如何在python中打印HTTP响应?

时间:2016-01-29 03:16:48

标签: python youtube

所以我正在编写这个我知道不会起作用的简单脚本,但脚本本身运行没有错误,并打电话给www.googleapis.com,所以发生了一些事情。但是,如果没有看到HTTP响应,我就不知道出了什么问题。它实际做某事的证据是因为我在中国,当我运行翻译时,有一段时间,直到我手动打破脚本:

OUTPUT WITHOUT PROXY :

 File "youtubeconnect.py", line 30, in <module>
    execute()
  File "youtubeconnect.py", line 27, in execute
    youtube = authenticate()
  File "youtubeconnect.py", line 15, 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)

现在,当我通过代理链运行解释器并对其进行隧道传输时,它可以工作:

ProxyChains-3.1 (http://proxychains.sf.net)
|DNS-request| www.googleapis.com 
|S-chain|-<>-127.0.0.1:1080-<><>-4.2.2.2:53-<><>-OK
|DNS-response| www.googleapis.com is 173.194.208.95
|S-chain|-<>-127.0.0.1:1080-<><>-173.194.208.95:443-<><>-OK

我是python的新手,当我尝试错误地连接到API时,如何获取谷歌发送给我的HTTP错误的输出?我的代码:

import httplib2
import os
from oauth2client import tools
from oauth2client.client import AccessTokenCredentials
#from oauth2client.client import AccessTokenRefreshError
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
import urllib
def authenticate():
    #scope = "https://www.googleapis.com/auth/youtube.upload"
    acc_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    flow = AccessTokenCredentials(
        acc_token,'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')
    http = flow.authorize(httplib2.Http())
    service = build('youtube', 'v3',http=http)
    return(service)


def initialize_upload(youtube):
    tags = None
    vid = youtube.videos().insert
    media_body = MediaFileUpload('1977.mp4', mimetype='video/mp4',
                                 chunksize=1024 * 1024, resumable=True)


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

execute()

1 个答案:

答案 0 :(得分:1)

使用记录:https://developers.google.com/api-client-library/python/guide/logging

此外,您可以使用Fiddler或Wireshark来分析发送给您的数据包。