Python協助函數調試函數 用於擷取對象的屬性及屬性值

來源:互聯網
上載者:User

標籤:share   proc   port   proxy   ade   har   string   templates   direct   

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




Python協助函數調試函數 用於擷取對象的屬性及屬性值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.