【问题标题】:add_header expecting 3 arguments instead of just key/valueadd_header 需要 3 个参数,而不仅仅是键/值
【发布时间】:2014-03-16 23:02:49
【问题描述】:

我遇到了这个错误信息:

TypeError: add_header() takes exactly 3 arguments (2 given)

使用这些参数时:

testService("SomeServiceName", "POST", "[redacted valid url]", ('Content-type','application/json'), [redacted valid json])

通常这个错误意味着我没有将“self”作为参数传递,但是看到这个方法不是在一个类中被调用,我不知道该怎么做。我尝试在参数和方法内部都将 self 作为参数传入。而且我尝试将标题包装在方括号和括号中。当我传递“self”时,我收到 self 未定义的错误消息,当我使用方括号而不是括号时,我收到与上面相同的错误。

有谁拥有神奇的 Python 调试技能吗?非常感谢您抽出宝贵时间查看此内容!

def testService(name, verb, url, header="", requestBody=""):

#Log out the name of the request we're testing
if (name is not None) or (name.strip() is not ""):
    print "Checking  " + name + "\n\n"

    # Make URL with StoreNumber
    if (url is not None) or (url is not ""):
        testUrl = url

        # If specified verb is GET
        if verb.strip().upper() == "GET":

            # Create request
            req = urllib2.Request(testUrl)
            print "Making request with URL: " + testUrl + "\n\n"

            # Send request
            try:
                response = urllib2.urlopen(req)

                # If service returns 200 Okay
                print "Connection to " + name + " Service successful. Returned with code " + str(response.code) + "\n\n"

                # Log response
                print "Response: " + response.read() + "\n\n"


                # Handle exceptions
                # If HTTP Error
            except HTTPError as e:
                if hasattr(e, 'reason'):
                    print name + ' failed to reach a server.'
                    print 'Reason: ', e.reason

                elif hasattr(e, 'code'):
                    print e.code

                elif hasattr(e, 'message'):
                    print e.message
                pass 

            # If URL was the problem
            except URLError as e:
                if hasattr(e, 'reason'):
                    print name + ' failed to reach a server.'

                    if str(e.reason) == "[Errno 11004] getaddrinfo failed":
                        print "[Errno 11004] getaddrinfo failed with bad url: " + testUrl + "\n\n"

                    else:
                        print 'Reason: ', e.reason

                elif hasattr(e, 'code'):
                    print 'Error code: ', e.code

                elif hasattr(e, 'message'):
                    print e.message
                pass 


        # If specified verb was POST
        elif verb.strip().upper() == "POST":

            # Check for None requestBody
            if (requestBody is not None) or (requestBody.strip() is not ""):
                data = urllib.urlencode(requestBody)

                # Create request
                req = urllib2.Request(testUrl, data)

                # Check for header
                if (header is not None) or (header.strip() is not ""):
                    req.add_header(header)

                    # YO YO THE BELOW CODE IS INCOMPLETE PLEASE FINISH
                    # Log request with URL and Data
                    print "Making request with URL: " + testUrl + " and data: THIS PART IS UNFINISHED PLEASE FINISH ME \n\n" 

                    try: 
                        response = urllib2.urlopen(req)

                        # If service returns 200 Okay
                        print "Connection to " + name + " Service successful. Returned with code " + str(response.code) + "\n\n"

                        # Log response
                        print "Response: " + response.read() + "\n\n"


                    # Handle exceptions
                    # If HTTP Error
                    except HTTPError as e:
                        if hasattr(e, 'code'):
                            print e.code
                        elif hasattr(e, 'message'):
                            print e.message
                        elif hasattr(e, 'reason'):
                            print name + ' failed to reach a server.'
                            print 'Reason: ', e.reason
                        pass 

                    except URLError as e:
                        if hasattr(e, 'reason'):
                            print name + ' failed to reach a server.'

                            if str(e.reason) == "[Errno 11004] getaddrinfo failed":
                                print "[Errno 11004] getaddrinfo failed with bad url: " + url + "\n\n"
                            else:
                                print 'Reason: ', e.reason                
                        elif hasattr(e, 'code'):
                            print 'Error code: ', e.code

                        elif hasattr(e, 'message'):
                            print e.message
                        pass 

                # Header non-existent in testService call
                else: 
                    print "Service header not provided. Exiting program"
                    sys.exit() 

            # Requesty Body not present in testService call
            else:
                print "Service request body not provided in code. Exiting program"
                sys.exit()

        # If specified verb is not supported (Currently only GET and POST are supported)    
        else:
            print name + " Service written with HTTP verb other than GET or POST. Exiting program"
            sys.exit()

    else:
        print "Service url not provided in code. Exiting program"
        sys.exit() 


else: 
    print "Service name not provided in code. Exiting program"
    sys.exit()

【问题讨论】:

    标签: python urllib2 request-headers


    【解决方案1】:

    documentationadd_header 接受两个参数。你用一个参数调用它,一个有两个值的元组。

    你应该做什么:

    req.add_header(key, value)
    

    您目前正在做什么,因为您将标头作为元组获取:

    req.add_header((key, value,))    # aka passing a tuple with both arguments to the key parameter
    

    你需要解压元组:

    req.add_header(header[0], header[1])
    

    或者甚至更好,使用splat 运算符 (*):

    req.add_header(*header)      # Does the same thing as above
    

    此外,您使用空字符串作为header 的默认参数,当它被提供时它是一个元组。您可能应该将默认值更改为元组或None

    【讨论】:

    • 完美运行。太感谢了!喜欢 splat 运算符。
    【解决方案2】:

    您的 header 是一个 2 元组:

    ('Content-Type', 'application/json')
    

    你正在尝试这样做:

    req.add_header('Content-Type', 'application/json')
    

    但实际上你正在这样做:

    req.add_header(('Content-Type', 'application/json'))
    

    请注意,您只传递了一个参数 - 一个元组 - 而不是两个,一个键和一个值。

    要解决此问题,请在使用 *(非正式地,“splat”)运算符传递 header 时解压它:

    req.add_header(*header)
    

    【讨论】:

      【解决方案3】:

      查看文档:http://docs.python.org/2/library/urllib2.html#urllib2.Request.add_header

      虽然函数需要一个键和一个值,但您只传递了一个对象。由于您是在 req 对象上调用 this,所以这也是正在传递的隐式“self”。

      你可以通过两种方式调用这个函数:

      req.add_header(key, value)
      urllib2.Request.add_header(req, key, value) # explicitly passing the reference instead of self
      

      我不确定您是否希望将传递的字符串视为键或值,但添加另一个参数(或使标头参数采用 dict,然后在 for 循环中适当拆分)应该解决问题。例如(删除不相关的代码):

      def testService(name, verb, url, header=None, requestBody=""):
      
          if header is None:
               header = {}
      
          for key, value in header.iteritems():
              req.add_header(key, value)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-18
        • 1970-01-01
        • 2021-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多