用Python实现简单的HTTP服务器(2)--实现一个基本功能的HTTP服务器
2012-01-17 22:36
1356 查看
总体还是很简单的, 大部分功能都是硬编码的, 不能进行灵活配置
默认加载主页index.html
页面不存在错误404, 还有服务器错误500
可以加载各种文件, 图片和文本文件
对请求参数进行简单处理
源代码如下
下载:http_server_demo.zip
下载后,运行server_demo.py这个文件就可以了
默认加载主页index.html
页面不存在错误404, 还有服务器错误500
可以加载各种文件, 图片和文本文件
对请求参数进行简单处理
源代码如下
# -.- coding:utf-8 -.- ''' Created on 2011-11-19 @author: icejoywoo ''' import socket import datetime import os s = socket.socket() host = socket.gethostname() port = 80 # 绑定服务器socket的ip和端口号 s.bind((host, port)) # 服务器名字/版本号 server_name = "MyServerDemo/0.1" # 缓存时间, 缓存一分钟 expires = datetime.timedelta(seconds=60) # GMT时间格式 GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT' # 可接收五个客户端 s.listen(5) print "You can see a HelloWorld from this server in ur browser, type in", host, "\r\n" # 服务器循环 while True: c, addr = s.accept() print "Got connection from", addr, "\r\n" print "--Request Header:" # 接收浏览器的请求, 不作处理 request = c.recv(1024) request_lines = request.split('\r\n') request_map = {} request_method = request_lines[0].split(' ')[0] # GET POST DELETE HEAD request_url = request_lines[0].split(' ')[1] request_http_ver = request_lines[0].split(' ')[2].split('/')[1] print '''request_method: %s request_url: %s request_http_version: %s ''' % (request_method, request_url, request_http_ver) # 提取头信息保存到字典中 for line in request_lines[1:-2]: if len(line) != 0: request_map[line.split(':')[0].strip()] = line.split(':')[1].strip() try: if request_url == "/": content = open("index.html", "rb").read() response_code = "200 OK" elif os.path.isdir(request_url[1:]): content = open(request_url[1:] + "/index.html", "rb").read() response_code = "200 OK" elif os.path.isfile(request_url[1:]): content = open(request_url[1:], "rb").read() response_code = "200 OK" else: response_code = "400 Not Found" # 文件未找到 content = "<h1>Page not found!</h1>" except: response_code = "500 Internal Error" # 执行有错误 content = "<h1>500, 你懂的!</h1>" # 获得请求的时间 now = datetime.datetime.utcnow() response = '''HTTP/1.1 %s Server: %s Date: %s Expires: %s Content-Type: text/html;charset=utf8 Content-Length: %s Connection: keep-alive %s''' % ( response_code, server_name, now.strftime(GMT_FORMAT), (now + expires).strftime(GMT_FORMAT), len(content), content ) # 发送回应 c.send(response) print "--Response:\r\n", response c.close()
下载:http_server_demo.zip
下载后,运行server_demo.py这个文件就可以了
BUG:倒数第二行的【print "--Response:\r\n", response】,加载图片或其他二进制文件的时候,会输出乱码,会有点问题,解决方法把这行注释了就可以
相关文章推荐
- 写一个python脚本,实现简单的http服务器功能:
- 分享一个iOS下实现基本绘画板功能的简单方法
- Python 实现一个简单的http服务器
- Python 实现一个简单的http服务器
- Python 实现一个简单的http服务器
- 用Python实现一个简单的能够上传下载的HTTP服务器
- Python探索之实现一个简单的HTTP服务器
- 用Python实现一个简单的能够上传下载的HTTP服务器
- java实现一个简单的HTTP服务器,带打开网页和计算功能
- 用Python 实现刷钻网上抢任务,并实现一个简单的限制使用时间的功能
- python实现简单http服务器功能
- 使用EF6和MVC5实现一个简单的选课系统--使用EF6实现基本的GRUD功能(2/12)
- 用Python socket实现一个简单的http服务器(post 与get 的区别)、CGIHTTPServer 简单应用
- Python 实现一个简单的http服务器
- 用最简单的脚本完成supertab的基本功能并实现一个更加合理的功能
- 用Python socket 实现一个简单的http服务器(post 与get 的区别)、CGI、WSGI、MVC
- Python 3.x--Socket实现简单的ssh和文件下载功能
- 使用Lucene实现一个简单的布尔搜索功能
- python实现简单爬虫功能
- yii2.0使用插件实现一个简单的上传功能