开发者社区> 微wx笑> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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

简介: Python帮助函数调试函数 用于获取对象的属性及属性值 刚接触Python,上篇 《Python入门》第一个Python Web程序——简单的Web服务器 中调试很不方便,不知道对象具体有什么属性,包含什么值,所以写了一个函数,用于获取对象的属性及属性值 函数代码如下: #调试函数,用于输出...
+关注继续查看

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

刚接触Python,上篇 《Python入门》第一个Python Web程序——简单的Web服务器 中调试很不方便,不知道对象具体有什么属性,包含什么值,所以写了一个函数,用于获取对象的属性及属性值

函数代码如下:

#调试函数,用于输出对象的属性及属性值
def getAllAttrs(obj):
	strAttrs = ''
	for o in dir(obj): 
		strAttrs =strAttrs + o + ' := ' + str(getattr(obj,o)) + '<br />'
		
	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)) + '<br />'
		
	return strAttrs;

#自定义处理程序,用于处理HTTP请求
class TestHTTPHandler(BaseHTTPRequestHandler):
	#处理GET请求
    def do_GET(self):
		#页面输出模板字符串
        templateStr = '''  
<html>  
<head>  
<title>QR Link Generator</title>  
</head>  
<body>  
%s
</body>  
</html> '''

	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 := 1
command := GET
connection := 
date_time_string := >
default_request_version := HTTP/0.9
disable_nagle_algorithm := False
do_GET := >
end_headers := >
error_content_type := text/html
error_message_format :=
Error response

Error 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.1
protocol_version := HTTP/1.0
raw_requestline := GET / HTTP/1.1 
rbufsize := -1
request := 
request_version := HTTP/1.1
requestline := GET / HTTP/1.1
responses := {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.3
setup := >
sys_version := Python/2.7.10
timeout := None
version_string := >
wbufsize := 0
weekdayname := ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
wfile := 




版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Python的类和对象
Python的类和对象
20 0
漫聊Go之GoVSPython|Go主题月
漫聊Go之GoVSPython|Go主题月
23 0
Python中可迭代对象怎么获取迭代器?
公众号新增加了一个栏目,就是每天给大家解答一道Python常见的面试题,反正每天不贪多,一天一题,正好合适,只希望这个面试栏目,给那些正在准备面试的同学,提供一点点帮助! 小猿会从最基础的面试题开始,每天一题。如果参考答案不够好,或者有错误的话,麻烦大家可以在留言区给出自己的意见和讨论,大家是要一起学习的 。
15 0
Python编程:获取一个类对象的属性和方法
Python编程:获取一个类对象的属性和方法
41 0
对象属性和类属性 | 手把手教你入门Python之五十八
通过类创建的对象被称为 实例对象,对象属性⼜称为实例属性,类属性就是类对象所拥有的属性,它被该类的所有实例对象所共有,类属性可以通过类对象或者实例对象访问。
947 0
Python函数、对象几个知识点
函数 一、参数 Python的函数参数可以定义默认值,当调用者没有提供参数时,函数在执行过程中用默认值设置该参数。 有默认值的参数必须声明在没有默认值的参数之后,例: def sum(x, y = 10): return x + y 除了按参数的...
833 0
+关注
微wx笑
无知人生,记录点滴。 不积跬步,无以至千里;不积小流,无以成江海&hellip;&hellip; 订阅号:微wx笑, 个人站点:https://www.ivu4e.com/, CSDN博客:https://blog.csdn.net/testcs_dn
1416
文章
1383
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载