Python实现简单的Web(续)
2016-12-03 17:10
225 查看
写的有点乱。。希望大神指教~~Python的缩进可真的将我缠了好久,想起我们班大神说缩进也是代码啊。。
修改之前的代码来显示请求的信息,同时重新整理一下代码:
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): # ... 页面模板 ... Page=".. 待设计.." def do_GET(self): page = self.create_page() self.send_content(page) def create_page(self): # ... 待实现 ... pass def send_content(self, page): # ... 待实现 ... pass
send_content 与之前 do_GET 内的代码一样:
def send_content(self, page): self.send_response(200) self.send_header("Content-type", "text/html") self.send_header("Content-Length", str(len(page))) self.end_headers() self.wfile.write(page)
设计页面模版
# 页面模板 Page = '''\ <html> <body> <p>Hello, this is my web!</p> <table border=1s> <tr> <td>Header</td> <td>Value</td> </tr> <tr> <td>Date and time</td> <td>{date_time}</td> </tr> <tr> <td>Client host</td> <td>{client_host}</td> </tr> <tr> <td>Client port</td> <td>{client_port}</td> </tr> <tr> <td>Command</td> <td>{command}</td> </tr> <tr> <td>Path</td> <td>{path}</td> </tr> </table> </body> </html> '''
实现 create_page
def create_page(self): values = { 'date_time': self.date_time_string(), 'client_host': self.client_address[0], 'client_port': self.client_address[1], 'command': self.command, 'path': self.path } page = self.Page.format(**values) return page
main 中的内容不用去修改它
结果。。。运行的结果是这样的。。
并没有返回正确信息。。。泪崩。。
先去解决错误了。。待会儿补上。。
啊啊啊,终于终于找到问题的根源所在了!!!!好激动好激动。
其实,出现上面的501的错误就是自己的代码有错误,代码错误,代码错误!!!如果不仔细检查的话,就一直深陷与此而止步了!!
在隔了一周将Java数据库编程和UML图书馆管理系统的bug测完后,将拉了一周的Python错误在今天就补上了。
回归正题:出现上述错误页面的原因如下:
1.首先要确定你的Python安装版本:
因为Python27与Python3.X中的网络编程还是有差异的。
我刚刚开始觉得是版本问题,于是在自己的电脑上装了Python2.7和Python3.6两个版本,
然后在又装了PyCharm这个美丽的IDE,相互调换着调试,最终决定用27版本的,然后将代码
一步一步认真的检查了后发现自己将
send_content在后面的调用中误写为sendContent。。。。。悲哀。。。所以最终认认真真改完代码就好了。
下篇正式将搭建的WEB写出来。
相关文章推荐
- WebTrack 在 python bottle framework中的简单实现
- python+mysql实现简单的web程序
- python+mysql实现简单的web程序
- Python基于twisted实现简单的web服务器
- Python基于twisted实现简单的web服务器
- 利用python2.7自带的简单的web服务器SimpleHTTPServer实现web页面的访问
- python 之 使用web.py实现一个简单的增删改查应用
- Python 实现简单 Web 服务器
- 使用Python或Node创建简单web服务器和FTP服务器实现文件共享
- Python SimpleHTTPServer 实现日志简单的web展示
- 使用Python简单的实现树莓派的WEB控制
- 使用Python简单的实现树莓派的WEB控制
- Python 实现简单的 Web
- Python实现简单的WEB服务器
- 循序渐进Python3(十二) --2-- web框架之django简单实现oracle数据库操作
- Python实现简单的Web完整版(一)
- Python(27)使用python的http,cgi模块实现一个简单的web服务
- 使用Python简单的实现树莓派的WEB控制
- python快速建立超简单的web服务器的实现方法
- Python Web简单加法器的实现--Python