- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我遇到了这个错误信息:
TypeError: add_header() 正好接受 3 个参数(给定 2 个)
使用这些参数时:
testService("SomeServiceName", "POST", "[编辑有效 url]", ('Content-type','application/json'), [编辑有效 json])
通常此错误意味着我没有将“self”作为参数传递,但看到此方法未在类中被调用,我不确定该怎么做。我试过在参数和方法内部将 self 作为参数传递。我试过将 header 括在方括号和圆括号中。当我传递“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()
最佳答案
来自documentation , add_header
接受两个参数。你用一个参数调用它,一个有两个值的元组。
你应该做什么:
req.add_header(key, value)
您当前正在做的事情是因为您将 header 作为元组获取:
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
。
关于python - add_header 期望 3 个参数而不只是键/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21832659/
我在远程机器上导航基于 Java 的 CLI 菜单,并在 bash 脚本中使用 expect,我试图从输出中提取某些内容而不离开 expect session 。 我的脚本中的 Expect 命令是:
我正在尝试使用 expect.h header 编译用 c 编写的程序。我正在尝试这个: cc -I/usr/include main.c -lexpect -ltcl cc -I/usr/inclu
我正在使用Expect与SSH session 和ERP程序进行自动交互。 而不是依靠正则表达式来捕获我期望脚本中的变量,是否有可能在收到用户的特定击键后将屏幕区域(例如一个字段)捕获为代码中的变量?
我是 PHP 面向对象编程的新手。我有个问题。我写了一个代码,但它不起作用。我知道这很容易,但我想知道它有什么问题。我出现以下错误: 当我尝试在另一个文件中使用它时,我现在遇到了这个错误:( 最佳答
声明了哪些出现了前所未见的错误,并试图找到解决方案。与以前的程序一样奇怪,它使用相同的语法但不会抛出任何错误 这是一个使用游标从表中检索信息,然后将其插入到另一个表中的过程,这样做是为了可以使用其中的
我已经用 CASE 编写了一个查询,但遇到了 () 问题。 select SM.subscriber_name as name , SM.accountType as accountTy
这个问题在这里已经有了答案: Why does removing return give me an error: expected type `()` but found type (1 个回答)
我有一个脚本可以登录服务器并执行一些命令。我需要能够从每个命令中检索返回代码,以确定脚本是否成功。我写了以下脚本,但没有按照我的意愿进行。目标是执行“cd/I/dont/exist”,这会产生错误代码
关闭。 这个问题需要 debugging details 。它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and
我正在运行一个 expect 脚本,它将生成一些来自 stdin 的动态输入。 是否有一种方法/模式可以解决从标准输入读取并将相关输入存储(?)到某处以在后面的步骤中处理/解析的概念? 示例: ./m
我正在运行一个 expect 脚本,该脚本在远程机器上调用多个脚本。这些 shell 脚本返回颜色输出(主要是红色和绿色)。问题是,那些颜色代码进入了我不想要的 log_file 和 STDOUT。我
我正在开发一个脚本,用于对软件安装进行回归测试。期望代码如下。前几行代码在浏览并同意许可证文件的地方运行良好。但是,脚本在“请输入有效许可证文件的路径名:”处停止,并且不执行任何操作。 (注意:手动安
我们创建以下简单的 expect 脚本以运行 netdata-installer.sh 预期脚本是: #!/usr/bin/expect set timeout 20 send "cd /tmp/ne
有人有T_PAAMAYIM_NEKUDOTAYIM吗? 最佳答案 是双冒号运算符 :: (见 list of parser tokens)。 关于PHP 期望 T_PAAMAYIM_NEKUDOTAY
我正在使用 Vercel SWR Hook usrSWR,我希望我可以将数据存储在某个遥远组件的缓存中,而不必使用上下文或其他一些全局状态管理器。 具体来说,我在 IndexPage 中使用 init
我刚刚注意到,如果我添加 if,Spock 不会断言条件。预期块中的子句,如 def myTest() { given: a = true expect: if ( a ) {
我有一个这样的方法: getValues(...args: Array) : Array { return args.map(k => { return this.shared
我正在使用 typescript + jest,并且在创建模拟实现时遇到了一些类型检查问题。例如,我想模拟 Credentials来自 aws-sdk 的对象: import { Credential
我依赖于一个以 Map 作为参数的方法。 public interface Service { void doSomething(Map map); } 我想写一个断言,用适当的 map 内容
我有一个适配器,它有一个方法,它采用可变参数列表,并将其转发给一个在我使用的框架中采用相同参数的方法。我想测试我的适配器是否正确转发了参数。然而,我不希望我的测试知道框架支持哪种参数。 我有一个工作期
我是一名优秀的程序员,十分优秀!