简体   繁体   English

TypeError:urlopen()获得了意外的关键字参数'headers'

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

I'm using rest api to send push notifications. 我正在使用rest api发送推送通知。 Docs are here. 文件在这里。 I'm using pyramid and scheduling these push notifications using celery. 我正在使用金字塔,并使用芹菜调度这些推送通知。

This is my code sample: 这是我的代码示例:

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())

But celery logs this error: 但是芹菜记录了这个错误:

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'

What is the problem? 问题是什么?

urllib2.urlopen has no argument named headers : urllib2.urlopen没有名为headers参数:

urllib2.urlopen: (url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT) urllib2.urlopen:(URL,数据=无,超时=套接字。_GLOBAL_DEFAULT_TIMEOUT)

Open the URL url, which can be either a string or a Request object. 打开URL URL,它可以是字符串或Request对象。

Use urllib2.Request to pass headers : 使用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)

Problem 问题

The method urllib2.urlopen has no headers argument. urllib2.urlopen没有headers参数。 This is what is indicated by the error message 这是错误消息指示的内容

TypeError: urlopen() got an unexpected keyword argument 'headers' TypeError:urlopen()获得了意外的关键字参数'headers'

Solution

The connection.request is the place where you define the headers . connection.request是定义headers的地方。

See this answer for an example. 有关示例,请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导航到一个 url 时,我收到错误:“urlopen() 得到了一个意外的关键字参数‘headers’” - when navigating to an url, I'm getting error: "urlopen() got an unexpected keyword argument 'headers' " TypeError:得到一个意外的关键字参数 - TypeError: got an unexpected keyword argument 类型错误:function() 得到了一个意外的关键字参数“njobs” - TypeError: function() got an unexpected keyword argument 'njobs' 类型错误:_log() 得到了意外的关键字参数“stacklevel” - TypeError: _log() got an unexpected keyword argument 'stacklevel' 类型错误:有一个意外的关键字参数“条目” - TypeError: got an unexpected keyword argument 'entry' TypeError:editProfile()得到了意外的关键字参数“ obj” - TypeError: editProfile() got an unexpected keyword argument 'obj' 类型错误:启动()有一个意外的关键字参数“超时” - TypeError: initiate() got an unexpected keyword argument 'timeout' 类型错误:tensor() 得到了一个意外的关键字参数“名称” - TypeError: tensor() got an unexpected keyword argument 'names' TypeError:recv()获得了意外的关键字参数“ dest” - TypeError: recv() got an unexpected keyword argument 'dest' 类型错误:videoResponsive() 得到了一个意外的关键字参数“宽度” - TypeError: videoResponsive() got an unexpected keyword argument 'width'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM