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 := 




目录
相关文章
|
8天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
13 1
|
3天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
28 0
|
3天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
4天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
4天前
|
Python
python学习10-函数
python学习10-函数
|
4天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
7天前
|
测试技术 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,用于修改函数或方法的行为。本文将深入探讨Python中装饰器的概念、用法和实际应用,以及如何利用装饰器实现代码的优雅和高效。
|
12天前
|
Python
Python函数学习应用案例详解
【4月更文挑战第7天】学习Python函数的应用,包括计算两数之和、判断偶数、计算阶乘、生成斐波那契数列及反转字符串。示例代码展示了函数接收参数和返回结果的功能,如`add(a, b)`求和,`is_even(num)`判断偶数,`factorial(n)`计算阶乘,`fibonacci(n)`生成斐波那契数,以及`reverse_string(s)`反转字符串。
13 1
|
13天前
|
Python
python基础篇:什么是函数?函数有什么用?
python基础篇:什么是函数?函数有什么用?
25 3
|
16天前
|
安全 Python
Python中input()函数
【4月更文挑战第3天】,`input()` 是 Python 内建函数,用于从控制台获取用户输入。它会暂停程序并显示提示信息(如果提供),用户输入的内容被视为字符串返回。基本语法是 `variable = input(prompt)`,其中 `prompt` 是可选提示信息。例如,`name = input("请输入您的姓名:")` 后,程序会等待用户输入,然后将输入的字符串赋值给 `name`。注意 `input()` 总是返回字符串,需手动转换为其他类型,且避免使用 `eval()` 处理用户输入以防止安全风险。
21 2
Python中input()函数