奇葩需求之-不使用python的第三方库做一个简单的网页
需求
最近接了一个特别奇葩的需求,要求在最基础的python2.7的docker环境上运行一个小型网站,不能使用额外的第三方库。因为之前都用flask,忽然不用库,有一点懵逼。然后找了一圈,发现用python自带的httpserver似乎可以完成这样的任务。代码如下:
代码
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/signin" method="post">
phone:<input type="text" name="phone"><br>
vscode:<input type="password" name="vscode"><br>
<input type="submit" value="login">
</form>
</body>
</html>
test.py
# -*- coding:utf-8 -*- # author: lichmama # email: nextgodhand@163.com # filename: httpd.py import io import os import sys import urllib from BaseHTTPServer import HTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler import urllib2 import json class MyRequestHandler(SimpleHTTPRequestHandler): protocol_version = "HTTP/1.1" server_version = "PSHS/0.1" sys_version = "Python/2.7.x" def get_evns(self): evns = os.environ return ' \\n '.join([k+"="+v for k,v in evns.items()]) def do_GET(self): if self.path == "/" or self.path == "/index": content = open("index.html", "rb").read() self.send_head(content) elif self.path == "/evns": content = self.get_evns() self.send_head(content) else: path = self.translate_path(self.path) system=sys.platform osv=os.environ if osv.has_key('HOMEPATH'): content = os.environ['HOMEPATH'] else: content = os.environ['HOME'] self.send_head(content) if os.path.exists(path): extn = os.path.splitext(path)[1].lower() content = open(path, "rb").read() self.send_head(content, type=self.extensions_map[extn]) else: content = open("404.html", "rb").read() self.send_head(content, code=404) self.send_content(content) def do_POST(self): if self.path == "/signin": data = self.rfile.read(int(self.headers["content-length"])) data = urllib.unquote(data) data = self.parse_data(data) test_data = {"username": "zhangyong_new","userpasswd": "123456"} requrl = "https://dev.honcloud.honeywell.com.cn/dashboard/usercentre/login" header = {"Content-type": "application/json"} test_data_urlencode=json.dumps(test_data) req = urllib2.Request(url=requrl, data=test_data_urlencode,headers=header) res_data = urllib2.urlopen(req) res = res_data.read() print res self.send_head(res) self.send_content(res) def parse_data(self, data): ranges = {} for item in data.split("&"): k, v = item.split("=") ranges[k] = v return ranges def send_head(self, content, code=200, type="text/html"): self.send_response(code) self.send_header("Content-Type", type) self.send_header("Content-Length", str(len(content))) self.end_headers() def send_content(self, content): f = io.BytesIO() f.write(content) f.seek(0) self.copyfile(f, self.wfile) f.close()
if __name__ == "__main__": if len(sys.argv) == 2: # set the target where to mkdir, and default "D:/web" MyRequestHandler.target = sys.argv[1] try: server = HTTPServer(("0.0.0.0", 8282), MyRequestHandler) print "pythonic-simple-http-server started, serving at http://%s:%d"%(server.server_address[0],server.server_address[1]) print server.server_address server.serve_forever() except KeyboardInterrupt: server.socket.close()