博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python帮助函数调试函数 用于获取对象的属性及属性值
阅读量:5112 次
发布时间:2019-06-13

本文共 5502 字,大约阅读时间需要 18 分钟。

Python帮助函数调试函数 用于获取对象的属性及属性值

刚接触Python,上篇  中调试非常不方便,不知道对象详细有什么属性,包括什么值,所以写了一个函数。用于获取对象的属性及属性值

函数代码例如以下:

#调试函数。用于输出对象的属性及属性值def getAllAttrs(obj):	strAttrs = ''	for o in dir(obj): 		strAttrs =strAttrs + o + ' := ' + str(getattr(obj,o)) + '
' return strAttrs;
详细应用代码:

import os	#Python的标准库中的os模块包括普遍的操作系统功能from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler  #导入HTTP处理相关的模块#调试函数。用于输出对象的属性及属性值def getAllAttrs(obj):	strAttrs = ''	for o in dir(obj): 		strAttrs =strAttrs + o + ' := ' + str(getattr(obj,o)) + '
' return strAttrs;#自己定义处理程序。用于处理HTTP请求class TestHTTPHandler(BaseHTTPRequestHandler): #处理GET请求 def do_GET(self): #页面输出模板字符串 templateStr = ''' QR Link Generator %s ''' self.protocal_version = 'HTTP/1.1' #设置协议版本号 self.send_response(200) #设置响应状态码 self.send_header("Welcome", "Contect") #设置响应头 self.end_headers() self.wfile.write(templateStr % getAllAttrs(self)) #输出响应内容 #启动服务函数def start_server(port): http_server = HTTPServer(('', int(port)), TestHTTPHandler) http_server.serve_forever() #设置一直监听并接收请求os.chdir('static') #改变工作文件夹到 static 文件夹start_server(8000) #启动服务。监听8000端口
输出例如以下:

MessageClass := mimetools.Message__doc__ := None__init__ := >__module__ := __main__address_string := >client_address := ('127.0.0.1', 38178)close_connection := 1command := GETconnection := date_time_string := >default_request_version := HTTP/0.9disable_nagle_algorithm := Falsedo_GET := >end_headers := >error_content_type := text/htmlerror_message_format :=Error responseError code %(code)d.Message: %(message)s.Error code explanation: %(code)s = %(explain)s. finish := >handle := >handle_one_request := >headers := Host: localhost:8000 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4,en-GB;q=0.2 Cookie: bdshare_firstime=1451130349627 log_date_time_string := >log_error := >log_message := >log_request := >monthname := [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']parse_request := >path := /protocal_version := HTTP/1.1protocol_version := HTTP/1.0raw_requestline := GET / HTTP/1.1 rbufsize := -1request := request_version := HTTP/1.1requestline := GET / HTTP/1.1responses := {200: ('OK', 'Request fulfilled, document follows'), 201: ('Created', 'Document created, URL follows'), 202: ('Accepted', 'Request accepted, processing continues off-line'), 203: ('Non-Authoritative Information', 'Request fulfilled from cache'), 204: ('No Content', 'Request fulfilled, nothing follows'), 205: ('Reset Content', 'Clear input form for further input.'), 206: ('Partial Content', 'Partial content follows.'), 400: ('Bad Request', 'Bad request syntax or unsupported method'), 401: ('Unauthorized', 'No permission -- see authorization schemes'), 402: ('Payment Required', 'No payment -- see charging schemes'), 403: ('Forbidden', 'Request forbidden -- authorization will not help'), 404: ('Not Found', 'Nothing matches the given URI'), 405: ('Method Not Allowed', 'Specified method is invalid for this resource.'), 406: ('Not Acceptable', 'URI not available in preferred format.'), 407: ('Proxy Authentication Required', 'You must authenticate with this proxy before proceeding.'), 408: ('Request Timeout', 'Request timed out; try again later.'), 409: ('Conflict', 'Request conflict.'), 410: ('Gone', 'URI no longer exists and has been permanently removed.'), 411: ('Length Required', 'Client must specify Content-Length.'), 412: ('Precondition Failed', 'Precondition in headers is false.'), 413: ('Request Entity Too Large', 'Entity is too large.'), 414: ('Request-URI Too Long', 'URI is too long.'), 415: ('Unsupported Media Type', 'Entity body in unsupported format.'), 416: ('Requested Range Not Satisfiable', 'Cannot satisfy request range.'), 417: ('Expectation Failed', 'Expect condition could not be satisfied.'), 100: ('Continue', 'Request received, please continue'), 101: ('Switching Protocols', 'Switching to new protocol; obey Upgrade header'), 300: ('Multiple Choices', 'Object has several resources -- see URI list'), 301: ('Moved Permanently', 'Object moved permanently -- see URI list'), 302: ('Found', 'Object moved temporarily -- see URI list'), 303: ('See Other', 'Object moved -- see Method and URL list'), 304: ('Not Modified', 'Document has not changed since given time'), 305: ('Use Proxy', 'You must use proxy specified in Location to access this resource.'), 307: ('Temporary Redirect', 'Object moved temporarily -- see URI list'), 500: ('Internal Server Error', 'Server got itself in trouble'), 501: ('Not Implemented', 'Server does not support this operation'), 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'), 503: ('Service Unavailable', 'The server cannot process the request due to a high load'), 504: ('Gateway Timeout', 'The gateway server did not receive a timely response'), 505: ('HTTP Version Not Supported', 'Cannot fulfill request.')}rfile := send_error := >send_header := >send_response := >server := server_version := BaseHTTP/0.3setup := >sys_version := Python/2.7.10timeout := Noneversion_string := >wbufsize := 0weekdayname := ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']wfile :=

转载于:https://www.cnblogs.com/claireyuancy/p/7091450.html

你可能感兴趣的文章
洛谷 1449——后缀表达式(线性数据结构)
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
Dirichlet分布深入理解
查看>>
(转)Android之发送短信的两种方式
查看>>
字符串处理
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
ad logon hour
查看>>
获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName...
查看>>
证件照(1寸2寸)拍摄处理知识汇总
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
Python入门-函数
查看>>
[HDU5727]Necklace(二分图最大匹配,枚举)
查看>>
距离公式汇总以及Python实现
查看>>
设计模式之装饰者模式
查看>>
一道不知道哪里来的容斥题
查看>>
Blender Python UV 学习
查看>>
window添加右键菜单
查看>>
入手腾龙SP AF90mm MACRO
查看>>
Window7上搭建symfony开发环境(PEAR)
查看>>