當前位置:
首頁 > 知識 > Python如何實現單步調試

Python如何實現單步調試

遇到大型python項目,如何定位問題和監控程序的運行狀態是一個程序員必須掌握的技能,今天小編為你帶來python程序的單步調試方法,方便易用,簡單易記!

首先你需要在所調試程序的開頭中:import pdb 並在你的代碼行設置斷點:pdb.set_trace

def get_input(Data, SuiteID, CaseID, caseinfolist):
global sArge
sArge=""
args =
#對於get請求,將參數組合
if reqmethod.upper=="GET":
for j in range(0, caseinfolist[1]):
if Data.read_data(SuiteID, Data.casebegin+CaseID, Data.argbegin+j) != "None":
ArgValue = Data.read_data(SuiteID, Data.casebegin+CaseID, Data.argbegin+j)
if "$$" in ArgValue:#走關聯分支
args = ArgValue.split("$$")
#print args
corvalue = Correl(args[0], args[1], args[2])
pdb.set_trace #######這兒設置斷點,程序運行到此處就開始了單步調試###########
if corvalue == :
sArge = "correlerr"
#return sArge
#infolog="關聯失敗"
#ret1 = "NG"
#Data.write_data(SuiteID, Data.casebegin+CaseID, 15,infolog,NG_COLOR)
#write_result(Date, SuiteID, Data.casebegin+CaseID, 16, ret1)
else:
sArge=sArge+caseinfolist[2][j]+"="+corvalue[0]+"&"
else:
sArge=sArge+caseinfolist[2][j]+"="+ArgValue+"&"
#print sArge
#去掉結尾的&字元
if sArge[-1:]=="&":
sArge = sArge[0:-1]
#sInput=caseinfolist[0]+sArge #為了post和get分開方便,不在這裡組合介面名,在調用的地方組合介面名。
return sArge
#對於post請求,因為不知道連接格式是=還是冒號,或者是其他的格式,所以不做拼接。直接取參數的第一個作為上傳body。
else:
sArge=Data.read_data(SuiteID, Data.casebegin+CaseID, 3)
if "$$" in sArge:#走關聯分支
args = sArge.split("$$")
#print args
corvalue = Correl(args[0], args[1], args[2])
if corvalue == :
sArge = "correlerr"
return sArge
else:
return sArge

程序開始之後pdb調試界面:

> c:userswangchaoworkspaceinterface_test estframe.py(253)HTTPInvoke
-> if reqmethod.upper=="GET":
(Pdb) l #執行命令l,會顯示出當前代碼的上下文,下面的『->』就是當前即將執行的代碼
248 def HTTPInvoke(url,requestUri):
249 proto,rest=urllib.splittype(url)
250 host,rest =urllib.splithost(rest)
251 conn = httplib.HTTPConnection(host)
252 pdb.set_trace
253 -> if reqmethod.upper=="GET":
254 print url
255 conn.request(reqmethod.upper, url,headers=reqHeaders)
256 rsps = conn.getresponse
257 if rsps.status==200:
258 data = rsps.read
(Pdb) reqmethod.upper #可以直接輸入相關變數名稱來查看當前變數的值
"GET"
(Pdb) n #n就是next的意思就是執行下一行
> c:userswangchaoworkspaceinterface_test estframe.py(254)HTTPInvoke
-> print url
(Pdb) b 260 #設置斷點
Breakpoint 1 at c:userswangchaoworkspaceinterface_test estframe.py:260
(Pdb) n
> c:userswangchaoworkspaceinterface_test estframe.py(254)HTTPInvoke
-> print url
(Pdb)
http://yue.sogou.com/api/h5/v1/history/recharge/list?pageNo=1&pageSize=1
> c:userswangchaoworkspaceinterface_test estframe.py(255)HTTPInvoke
-> conn.request(reqmethod.upper, url,headers=reqHeaders)
(Pdb) s # 上面通過執行n程序運行到調用函數的地方,使用s可以進入函數內部
--Call--
> c:python27libhttplib.py(1040)request
-> def request(self, method, url, body=None, headers={}):
(Pdb) a # 顯示當前所有棧變數的值
self =
method = GET
url = http://yue.sogou.com/api/h5/v1/history/recharge/list?pageNo=1&pageSize=1
body = None
headers = {"Connection": "keep-alive", "Cookie": "SUV=004C0C0F6FCA67CB585CD8F53FC5D135; CXID=647977743F187E018526B8ECA587A052; IPLOC=CN1100; OPENID=6D4569C5D00A35876E60A94E34D685AD; pgv_pvi=2641157120; _ga=GA1.2.2141014782.1484890447; ssuid=5496173722; SMYUV=1485154923097202; sgsa_id=sogou.com|1486180882291442; GOTO=Af99046; clientId=291C8B8E05B2647F206981AD04136539; JSESSIONID=aaa_iPLt7BVjahJ1G5GOv; SNUID=FB57E5402F2A672762F7CB303085C13A; ld=uRGSyZllll2YYa4WlllllVAtUeYlllllWTieMkllll9llllljylll5@@@@@@@@@@; LSTMV=316%2C266; LCLKINT=3486; sct=61; ad=jkllllllll2Yg2o@lllllVANab9lllllbDIXhZllllwllllljOxlw@@@@@@@@@@@; SUID=7BC6CA017F430E0A0000000052256039; YYID=AA6E39EBC7D6CC4AA0839B4929E7511C; usid=pp63hL5QOQSxi2gw; sgid=AViadNOrPzjYb8SzAw5wsq5g", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "User-Agent": "Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0"}
(Pdb) l
1035 self.__state = _CS_REQ_SENT
1036 else:
1037 raise CannotSendHeader
1038 self._send_output(message_body)
1039
1040 -> def request(self, method, url, body=None, headers={}):
1041 """Send a complete request to the server."""
1042 self._send_request(method, url, body, headers)
1043
1044 def _set_content_length(self, body, method):
1045 # Set the content-length based on the body. If the body is "empty", we
(Pdb) r #命令r返回到前面所進入函數的末尾
--Return--
> c:python27libhttplib.py(1042)request->None
-> self._send_request(method, url, body, headers)
(Pdb) l # 可以通過l驗證一下當前程序執行的位置
1037 raise CannotSendHeader
1038 self._send_output(message_body)
1039
1040 def request(self, method, url, body=None, headers={}):
1041 """Send a complete request to the server."""
1042 -> self._send_request(method, url, body, headers)
1043
1044 def _set_content_length(self, body, method):
1045 # Set the content-length based on the body. If the body is "empty", we
1046 # set Content-Length: 0 for methods that expect a body (RFC 7230,
1047 # Section 3.3.2). If the body is set for other methods, we set the
(Pdb) r # 再執行r返回到調用該函數的地方
> c:userswangchaoworkspaceinterface_test estframe.py(256)HTTPInvoke
-> rsps = conn.getresponse
(Pdb) l
251 conn = httplib.HTTPConnection(host)
252 pdb.set_trace
253 if reqmethod.upper=="GET":
254 print url
255 conn.request(reqmethod.upper, url,headers=reqHeaders)
256 -> rsps = conn.getresponse
257 if rsps.status==200:
258 data = rsps.read
259 data = str(data)
260 B conn.close
261 return data
(Pdb) c #執行命令c繼續運行程序,直到斷點就停留在此位置,上面設置斷點的命令「b 260」260表示第多少行
> c:userswangchaoworkspaceinterface_test estframe.py(260)HTTPInvoke
-> conn.close
(Pdb) l
255 conn.request(reqmethod.upper, url,headers=reqHeaders)
256 rsps = conn.getresponse
257 if rsps.status==200:
258 data = rsps.read
259 data = str(data)
260 B-> conn.close
261 return data
262 elif rsps.status==301 or rsps.status==302:
263 headerstr=rsps.getheaders
264 for i in headerstr:
265 if i[0].lower=="location":
(Pdb) pp data # pp 列印某個變數的值
"{"code":0,"data":{"pageCount":0,"pageList":,"pageNo":1,"pageSize":1,"totalCount":0},"msg":"xe6x88x90xe5x8ax9f"}"
(Pdb)

總結:上面的程序是本人私有,因而不能全部上傳,在練習pdb時,建議使用自己的程序,pdb單步調試方法總結如下:

命令


解釋
break 或 b 設置斷點 設置斷點
continue 或 c 繼續執行程序
list 或 l 查看當前行的代碼段
step 或 s 進入函數
return 或 r 執行代碼直到從當前函數返回
exit 或 q 中止並退出
next 或 n 執行下一行
pp 列印變數的值
a 查看全部棧內變數

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

Carbondata源碼系列(一)文件生成過程
直截了當的告訴你什麼是工廠模式
React 實踐項目(三)

TAG:達人科技 |

您可能感興趣

iOS 下如何一鍵調試 Push
使用pdb進行Python調試
Servlet 調試
Python 調試器入門
如何使用Ghostscript調試PostScript
如何攻克 Android 調試難題?
你真的會正確地調試TensorFlow代碼嗎?
PyTorch代碼調試利器:自動print每行代碼的Tensor信息
strace幫助你調試PHP代碼
Python錯誤、調試和測試
Chrome 調試技巧
NI推出InstrumentStudio軟體 簡化了自動化測試系統的開發和調試
如何在 Linux 或者 UNIX 下調試 Bash Shell 腳本
如何使用curl調試openstack的api
linux性能調試之iostat
使用pdb進行Python調試(下篇)
使用systemtap調試工具分析MySQL的性能
IntelliJ遠程調試詳解
Android遠程調試Web頁面
windbg藍屏調試