0
默認情況下,SimpleHTTPServer發送它自己的頭文件。SimpleHTTPServer自定義頭文件
我一直想弄清楚如何發送我自己的標題,發現this solution。我嘗試了適應我的非常簡單的代理:
import SocketServer
import SimpleHTTPServer
import urllib
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
headers = ['Date: Wed, 29 Oct 2014 15:54:43 GMT', 'Server: Apache', 'Accept-Ranges: bytes', 'X-Mod-Pagespeed: 1.6.29.7-3566', 'Vary: Accept-Encoding', 'Cache-Control: max-age=0, no-cache', 'Content-Length: 204', 'Connection: close', 'Content-Type: text/html']
def end_headers(self):
print "Setting custom headers"
self.custom_headers()
SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
def custom_headers(self):
for i in self.headers:
key, value = i.split(":", 1)
self.send_header(key, value)
def do_GET(self):
self.copyfile(urllib.urlopen(self.path), self.wfile)
httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy)
httpd.serve_forever()
但end_headers()
不設置自定義頁眉(證實Wireshark的)。
給出一個headers
的列表,就像我的小片段中的一個,我如何覆蓋SimpleHTTPServer's
默認的標題和服務器我自己?
[可能的重複](http://stackoverflow.com/questions/22308786/python-simplehttpserver-change-response-header/22309792#22309792)? – Noelkd 2014-10-29 17:06:13