Python helps function debugging function to get object attributes and attribute values
Python helps function debugging function to get object attributes and attribute values
I just got started with Python and didn't know what attributes and values the object has. So I wrote a function to get the attributes and attribute values of the object.
The function code is as follows:
# Debug function, used to output object attributes and attribute values def getAllAttrs (obj): strAttrs = ''for o in dir (obj): strAttrs = strAttrs + o + ': = '+ str (getattr (obj, o) + ''return strAttrs;
Specific application code:
Import OS # The OS module in the Python standard library contains common operating system functions from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler # import HTTP processing-related modules # debug functions, def getAllAttrs (obj): strAttrs = ''for o in dir (obj): strAttrs = strAttrs + o + ': = '+ str (getattr (obj, o) + ''return strAttrs; # Custom Handler used to process HTTP request class TestHTTPHandler (BaseHTTPRequestHandler ): # Processing GET request def do_GET (self): # templateStr = '''
% S ''' self. protocal_version = 'HTTP/1.1 '# sets the Protocol version self. send_response (200) # Set the response status code self. send_header ("Welcome", "Contect") # sets the Response Header self. end_headers () self. wfile. write (templateStr % getAllAttrs (self) # output response content # Start the service function def start_server (port): http_server = HTTPServer ('', int (port), TestHTTPHandler) http_server.serve_forever () # Set to listen continuously and receive requests from the OS. chdir ('static ') # change the working directory to the static directory start_server (8000) # Start the service and listen to port 8000
The output is as follows:
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 :=