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

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

函数代码例如以下:

  1. #调试函数。用于输出对象的属性及属性值
  2. def getAllAttrs(obj):
  3. strAttrs = ''
  4. for o in dir(obj):
  5. strAttrs =strAttrs + o + ' := ' + str(getattr(obj,o)) + '<br />'
  6.  
  7. return strAttrs;

详细应用代码:

  1. import os #Python的标准库中的os模块包括普遍的操作系统功能
  2.  
  3. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler #导入HTTP处理相关的模块
  4.  
  5. #调试函数。用于输出对象的属性及属性值
  6. def getAllAttrs(obj):
  7. strAttrs = ''
  8. for o in dir(obj):
  9. strAttrs =strAttrs + o + ' := ' + str(getattr(obj,o)) + '<br />'
  10.  
  11. return strAttrs;
  12.  
  13. #自己定义处理程序。用于处理HTTP请求
  14. class TestHTTPHandler(BaseHTTPRequestHandler):
  15. #处理GET请求
  16. def do_GET(self):
  17. #页面输出模板字符串
  18. templateStr = '''
  19. <html>
  20. <head>
  21. <title>QR Link Generator</title>
  22. </head>
  23. <body>
  24. %s
  25. </body>
  26. </html> '''
  27.  
  28. self.protocal_version = 'HTTP/1.1' #设置协议版本号
  29. self.send_response(200) #设置响应状态码
  30. self.send_header("Welcome", "Contect") #设置响应头
  31. self.end_headers()
  32. self.wfile.write(templateStr % getAllAttrs(self)) #输出响应内容
  33.  
  34. #启动服务函数
  35. def start_server(port):
  36. http_server = HTTPServer(('', int(port)), TestHTTPHandler)
  37. http_server.serve_forever() #设置一直监听并接收请求
  38.  
  39. os.chdir('static') #改变工作文件夹到 static 文件夹
  40. start_server(8000) #启动服务。监听8000端口

输出例如以下:

  1. MessageClass := mimetools.Message
  2. __doc__ := None
  3. __init__ := >
  4. __module__ := __main__
  5. address_string := >
  6. client_address := ('127.0.0.1', 38178)
  7. close_connection := 1
  8. command := GET
  9. connection :=
  10. date_time_string := >
  11. default_request_version := HTTP/0.9
  12. disable_nagle_algorithm := False
  13. do_GET := >
  14. end_headers := >
  15. error_content_type := text/html
  16. error_message_format :=
  17. Error response
  18.  
  19. Error code %(code)d.
  20.  
  21. Message: %(message)s.
  22.  
  23. Error code explanation: %(code)s = %(explain)s.
  24. finish := >
  25. handle := >
  26. handle_one_request := >
  27. 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
  28. log_date_time_string := >
  29. log_error := >
  30. log_message := >
  31. log_request := >
  32. monthname := [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  33. parse_request := >
  34. path := /
  35. protocal_version := HTTP/1.1
  36. protocol_version := HTTP/1.0
  37. raw_requestline := GET / HTTP/1.1
  38. rbufsize := -1
  39. request :=
  40. request_version := HTTP/1.1
  41. requestline := GET / HTTP/1.1
  42. 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.')}
  43. rfile :=
  44. send_error := >
  45. send_header := >
  46. send_response := >
  47. server :=
  48. server_version := BaseHTTP/0.3
  49. setup := >
  50. sys_version := Python/2.7.10
  51. timeout := None
  52. version_string := >
  53. wbufsize := 0
  54. weekdayname := ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  55. wfile :=

Python帮助函数调试函数 用于获取对象的属性及属性值的更多相关文章

  1. Python面向对象 -- 继承和多态、获取对象信息、实例属性和类属性

    继承和多态 继承的好处: 1,子类可以使用父类的全部功能 2,多态:当子类和父类都存在相同的方法时,子类的方法会覆盖父类的方法,即调用时会调用子类的方法.这就是继承的另一个好处:多态. 多态: 调用方 ...

  2. python中的is判断引用的对象是否一致,==判断值是否相等

    python中的is判断引用的对象是否一致,==判断值是否相等 a = 10 b = 20 list = [1,2,3,4,5] print(a in list) print(b not in lis ...

  3. 【Python】[面性对象编程] 获取对象信息,实例属性和类属性

    获取对象信息1.使用isinstance()判断class类型2.dir() 返回一个对象的所有属性和方法3.如果试图获取不存在的对象会抛出异常[AttributeError]4.正确利用对象内置函数 ...

  4. 【Javascript-基础-getOwnPropertyNames】Object.getOwnPropertyNames() 获取对象自身可枚举属性

    可枚举属性和不可枚举属性 在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for-in查找遍历到. 可枚举属性 e ...

  5. PHP获取对象的纯数字属性

    php的对象属性 我们知道获取php的对象属性用箭头: echo $obj->name; 如果属性名是一个变量,那么可以用: $var = 'name'; echo $obj->$var; ...

  6. JavaScript 获取对象中第一个属性

    使用 Object.keys(object) 可以取出属性名为数组,但会打乱顺序 严格意义上对象中是只有映射关系而没有顺序的,但是在存储结构里是有顺序的,如果想获取存储结构里的第一个属性可以使用for ...

  7. 校验正确获取对象或者数组的属性方法(babel-plugin-idx/_.get)

    背景: 开发中经常遇到取值属性的时候,需要校验数值的有效性. 例如: 获取props对象里面的friends属性 props.user && props.user.friends &a ...

  8. Stream流用于按照对象中某一属性来对集合去重+简单数据类型集合的去重

    上次对Stream流来进行分组的文章很多人看,想看的可以来这: Stream流来进行集合分组 这次小编又带来Stream的去重,话不多数,直接上代码: 这是对简单数据类型的去重 //字符串集合进行简单 ...

  9. python的operator.itemgetter('click')用于定义获取'click'项的函数

    python的排序参见文章http://blog.csdn.net/longshenlmj/article/details/12747195 这里介绍 import operator模块 operat ...

随机推荐

  1. python之 文件操作

    一.初识文件操作 使用python来读写文件是非常简单的操作,我们使用open函数来打开一个文件,获取到 文件句柄,然后通过文件句柄就可以进行各种各样的操作,同过打开方式的不同能够执行的 操作也会有相 ...

  2. Combox两级联动会经常出现的错误

    例如: 当我们遇到这种情况:(下拉框的隐藏值和显示值皆为实体类进行绑定值时)下拉框的隐藏值并不能成功获取到. 我们就可以使用下面 的方案来解决 ok ,成功获取到隐藏值. 还有一个,附加解决方案:

  3. Spring.net(v1.3.2) 官网API-第一章 前言

    虽然有很好的工具和技术,但是开发软件应用仍然是很困难的.Spring为构建企业级应用提供了一个轻量级的解决方案,Spring提供了一个统一的.透明的方式去配置你的应用,和将AOP集成到你的软件中.Sp ...

  4. [转][IPC通信]基于管道的popen和pclose函数

    标准I/O函数库提供了popen函数,它启动另外一个进程去执行一个shell命令行. 这里我们称调用popen的进程为父进程,由popen启动的进程称为子进程. popen函数还创建一个管道用于父子进 ...

  5. JS高级——函数的调用模式

    函数调用模式一共有四种 <script> //1.函数模式 //this指向window全局对象 //2.方法模式 //this指向调用这个方法的对象 //3.构造函数模式 //this ...

  6. CAD在网页中打印的图纸里面添加页眉及页脚

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  7. Spring处理自动装配的歧义性

    1.标识首选的bean 2.使用限定符@Qualifier 首先在bean的声明上添加@Qualifier 注解: @Component @Qualifier("cdtest") ...

  8. css3 animation 中的 steps

    steps Specifies a stepping function, described above, taking two parameters. The first parameter spe ...

  9. BZOJ 2850: 巧克力王国 KDtree + 估价函数

    Code: #include<bits/stdc++.h> #define maxn 100000 #define inf 1000000008 #define mid ((l+r)> ...

  10. js兼用性

    1.document.formName.item("itemName") 问题 说明:IE下,可以使用document.formName.item("itemName&q ...