首页 新闻 会员 周边

python 中BaseHTTPServer 问题

0
悬赏园豆:50 [已关闭问题] 关闭于 2013-04-01 14:32

使用BaseHTTPServer写一个简单的服务器,实现的功能很简单,只是把其中的html文件显示出来:

 1 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
 2 from os import sep, curdir
 3 import cgi
 4 
 5 
 6 PORT = 8080
 7     
 8 class myHandler(BaseHTTPRequestHandler):
 9             
10     def do_GET(self):
11 
12         if self.path == '/':
13             self.path = '\\test.html'
14             
15         try:
16             reply = False
17             if self.path.endswith('.html'):
18                 reply = True
19                 mimeType = 'text/html'
20                 
21             if self.path.endswith('.jpg'):
22                 reply = True
23                 mimeType = 'image.jpg'
24                 
25             if self.path.endswith('.js'):
26                 reply = True
27                 mimeType = 'application/javascript'
28                 
29             if self.path.endswith('.txt'):
30                 reply = True
31                 mimeType = 'text/txt'
32                 
33             
34             if(reply == True):
35                 fp = open(curdir + sep + self.path)
36                 self.send_response(200)
37                 self.send_header('content-type', mimeType)
38                 self.end_headers()
39                 self.wfile.write(fp.read())
40                 fp.close()
41             return
42         except IOError:
43             self.send_error(404, 'Not Found File %s' %self.path);
44         
45     def do_POST(self):
46         form = cgi.FieldStorage(
47                                 fp = self.rfile,
48                                 headers = self.headers,
49                                 environ = {
50                                            'REQUEST_METHOD':'POST',
51                                            'CONTENT_TYPE':self.headers.getheader('current-type')
52                                            }
53                                 )
54         
55         print form
56         
57         
58 
59         #self.wfile.write(form['name'])
60 
61     
62 try:
63     ser = HTTPServer(('', PORT), myHandler) 
64     print '\n\nStart HTTP Server at PORT:' , PORT
65     ser.serve_forever()
66 except KeyboardInterrupt:
67     print 'Shutting down the server!!'
68     ser.socket.close()

 

 

假如html文件中有引用到script css文件,或者其他的webm等类型的video视频,用BaseHTTPServer只能显示出html页面,但是其中并不会调用js脚本和css文件(html代码中已经包含了

1     <head>
2         <title>HTML5 video</title>
3         <link type='text/css' rel='stylesheet' href='style.css'>
4         <script src='script.js' type='application/javascript'></script>
5     </head>

),,webm的视频也显示不出来,页面报错找不到这个类型(但是HTML5其实是支持的,使用SimpleHTTPServer都可以正常显示),但是SimpleHTTPServer都可以,是不是因为BaseHTTPServer比较基本,还需要在GET中加入parse的成分?

二师兄89的主页 二师兄89 | 菜鸟二级 | 园豆:204
提问于:2013-03-25 13:44
< >
分享
所有回答(1)
0

这个问题怎么解决的

bryan151 | 园豆:204 (菜鸟二级) | 2017-09-07 23:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册