用Python实现简单的HTTP服务器(2)--实现一个基本功能的HTTP服务器
2012-01-17 22:36 会被淹死的鱼 阅读(816) 评论(0) 编辑 收藏 举报总体还是很简单的, 大部分功能都是硬编码的, 不能进行灵活配置
- 默认加载主页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()
下载后,运行server_demo.py这个文件就可以了
BUG:倒数第二行的【print "--Response:\r\n", response】,加载图片或其他二进制文件的时候,会输出乱码,会有点问题,解决方法把这行注释了就可以
![](http://pic.cnblogs.com/face/u216123.jpg)
作者:icejoywoo
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
短网址: http://goo.gl/ZiZCi