簡體   English   中英

TypeError:urlopen()獲得了意外的關鍵字參數'headers'

[英]TypeError: urlopen() got an unexpected keyword argument 'headers'

我正在使用rest api發送推送通知。 文件在這里。 我正在使用金字塔,並使用芹菜調度這些推送通知。

這是我的代碼示例:

result = urllib2.urlopen(urlRequest, headers={
      "X-Parse-Application-Id": settings["parse.application.id"],
      "X-Parse-REST-API-Key": settings["parse.restapi.key"],
      "Content-Type": "application/json"
     })

connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()

connection.request('POST', '/1/push', json.dumps(data), )
result = json.loads(connection.getresponse().read())

但是芹菜記錄了這個錯誤:

2015-08-18 16:39:45,092 INFO  [celery.worker.strategy][MainThread] Received task: app_v1_1.tasks.push_notification[877906d8-1ea7-4b1f-8a54-aa61bffb40e8]
2015-08-18 16:39:45,094 ERROR [celery.worker.job][MainThread] Task app_v1_1.tasks.push_notification[877906d8-1ea7-4b1f-8a54-aa61bffb40e8] raised unexpected: TypeError("urlopen() got an unexpected keyword argument 'headers'",)
Traceback (most recent call last):
  File "/home/apnistreet/work/ve/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/comp/work/ve/local/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/comp/work/site/code/apnistreet_v1_1/tasks.py", line 168, in push_notification
    # "Content-Type": "application/json"
TypeError: urlopen() got an unexpected keyword argument 'headers'

問題是什么?

urllib2.urlopen沒有名為headers參數:

urllib2.urlopen:(URL,數據=無,超時=套接字。_GLOBAL_DEFAULT_TIMEOUT)

打開URL URL,它可以是字符串或Request對象。

使用urllib2.Request傳遞headers

req = urllib2.Request(url, headers={
      "X-Parse-Application-Id": settings["parse.application.id"],
      "X-Parse-REST-API-Key": settings["parse.restapi.key"],
      "Content-Type": "application/json"
     })
result = urllib2.urlopen(req)

問題

urllib2.urlopen沒有headers參數。 這是錯誤消息指示的內容

TypeError:urlopen()獲得了意外的關鍵字參數'headers'

connection.request是定義headers的地方。

有關示例,請參見此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM