python的函数的对象属性_Python帮助函数调试函数 用于获取对象的属性及属性值...
Python幫助函數(shù)調(diào)試函數(shù) 用于獲取對(duì)象的屬性及屬性值
剛接觸Python,上篇 《Python入門》第一個(gè)Python Web程序——簡單的Web服務(wù)器?中調(diào)試非常不方便,不知道對(duì)象詳細(xì)有什么屬性,包括什么值,所以寫了一個(gè)函數(shù)。用于獲取對(duì)象的屬性及屬性值
函數(shù)代碼例如以下:
#調(diào)試函數(shù)。用于輸出對(duì)象的屬性及屬性值
def getAllAttrs(obj):
strAttrs = ‘‘
for o in dir(obj):
strAttrs =strAttrs + o + ‘ := ‘ + str(getattr(obj,o)) + ‘
‘
return strAttrs;詳細(xì)應(yīng)用代碼:
import os#Python的標(biāo)準(zhǔn)庫中的os模塊包括普遍的操作系統(tǒng)功能
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler #導(dǎo)入HTTP處理相關(guān)的模塊
#調(diào)試函數(shù)。用于輸出對(duì)象的屬性及屬性值
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‘#設(shè)置協(xié)議版本號(hào)
self.send_response(200)#設(shè)置響應(yīng)狀態(tài)碼
self.send_header("Welcome", "Contect")#設(shè)置響應(yīng)頭
self.end_headers()
self.wfile.write(templateStr % getAllAttrs(self))#輸出響應(yīng)內(nèi)容
#啟動(dòng)服務(wù)函數(shù)
def start_server(port):
http_server = HTTPServer((‘‘, int(port)), TestHTTPHandler)
http_server.serve_forever()#設(shè)置一直監(jiān)聽并接收請求
os.chdir(‘static‘)#改變工作文件夾到 static 文件夾
start_server(8000)#啟動(dòng)服務(wù)。監(jiān)聽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 :=
總結(jié)
以上是生活随笔為你收集整理的python的函数的对象属性_Python帮助函数调试函数 用于获取对象的属性及属性值...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python数据模型搭建_python之
- 下一篇: unity 继承会调用start吗_Un