我正在尝试向服务器发出http请求,并检查返回的内容。然而,当我尝试用ipdb
来查找HTTPResponse object
时,我一直得到*** Oldest frame
,并且我无法在对象上运行任何我应该能够运行的函数。以下是获取的代码块,以及ipdb
输出:
代码块:
for acc in sp_lost:
url = 'http://www.uniprot.org/uniprot/?query=mnemonic%3a'+acc+'+active%3ayes&format=tab&columns=entry%20name'
u = urllib.request.urlopen(url)
ipdb.set_trace()
ipdb输出:
ipdb> url
'http://www.uniprot.org/uniprot/?query=mnemonic%3aSPATL_MOUSE+active%3ayes&format=tab&columns=entry%20name'
ipdb> u
*** Oldest frame
ipdb> str(u)
'<http.client.HTTPResponse object at 0xe58e2d0>'
ipdb> type(u)
<class 'http.client.HTTPResponse'>
ipdb> u.url
*** Oldest frame
ipdb> u.url() # <-- unable to run url() on object...?
*** Oldest frame
ipdb>
*** Oldest frame
是什么意思?我如何将这个对象转化为更有用的东西,以便运行适当的函数?
u
是遍历堆栈帧的PDB命令。您已经处于"最上面"的帧中。help u
会告诉你更多关于它的信息:
u(p) Move the current frame one level up in the stack trace (to an older frame).
该命令与d(own)
和w(here)
:密切相关
d(own) Move the current frame one level down in the stack trace (to a newer frame). w(here) Print a stack trace, with the most recent frame at the bottom. An arrow indicates the "current frame", which determines the context of most commands. 'bt' is an alias for this command.
如果要打印变量u
,请在其前面加上!
,使调试器不会将其解释为调试命令:
!u
!u.url
或使用print()
:
print(u)
来自help pdb
输出:
调试器无法识别的命令被假定为Python语句,并在程序的上下文中执行调试。Python语句也可以以感叹号为前缀点('!')。
Oldest frame
是堆栈中程序启动的帧;它是历史上最古老的;堆栈的另一端Newest frame
是Python执行代码的地方,也是当前执行帧,如果您点击c(ontinue)
命令,Python将继续执行。
一个带有递归函数的小演示:
>>> def foo():
... foo()
...
>>> import pdb
>>> pdb.run('foo()')
> <string>(1)<module>()
(Pdb) s
--Call--
> <stdin>(1)foo()
(Pdb) s
> <stdin>(2)foo()
(Pdb) s
--Call--
> <stdin>(1)foo()
(Pdb) s
> <stdin>(2)foo()
(Pdb) s
--Call--
> <stdin>(1)foo()
(Pdb) w
/Users/mj/Development/Libraries/buildout.python/parts/opt/lib/python2.7/bdb.py(400)run()
-> exec cmd in globals, locals
<string>(1)<module>()
<stdin>(2)foo()
<stdin>(2)foo()
> <stdin>(1)foo()
(Pdb) u
> <stdin>(2)foo()
(Pdb) u
> <stdin>(2)foo()
(Pdb) u
> <string>(1)<module>()
(Pdb) u
> /Users/mj/Development/Libraries/buildout.python/parts/opt/lib/python2.7/bdb.py(400)run()
-> exec cmd in globals, locals
(Pdb) u
*** Oldest frame