{"id":360,"date":"2019-01-10T09:40:15","date_gmt":"2019-01-10T01:40:15","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=360"},"modified":"2022-05-17T09:44:11","modified_gmt":"2022-05-17T01:44:11","slug":"python-4","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=360","title":{"rendered":"\u5947\u8469\u9700\u6c42\u4e4b-\u4e0d\u4f7f\u7528python\u7684\u7b2c\u4e09\u65b9\u5e93\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u9875"},"content":{"rendered":"<h1>\u9700\u6c42<\/h1>\n<p>\u6700\u8fd1\u63a5\u4e86\u4e00\u4e2a\u7279\u522b\u5947\u8469\u7684\u9700\u6c42\uff0c\u8981\u6c42\u5728\u6700\u57fa\u7840\u7684python2.7\u7684docker\u73af\u5883\u4e0a\u8fd0\u884c\u4e00\u4e2a\u5c0f\u578b\u7f51\u7ad9\uff0c\u4e0d\u80fd\u4f7f\u7528\u989d\u5916\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u56e0\u4e3a\u4e4b\u524d\u90fd\u7528flask,\u5ffd\u7136\u4e0d\u7528\u5e93\uff0c\u6709\u4e00\u70b9\u61f5\u903c\u3002\u7136\u540e\u627e\u4e86\u4e00\u5708\uff0c\u53d1\u73b0\u7528python\u81ea\u5e26\u7684httpserver\u4f3c\u4e4e\u53ef\u4ee5\u5b8c\u6210\u8fd9\u6837\u7684\u4efb\u52a1\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<h1>\u4ee3\u7801<\/h1>\n<p>index.html<\/p>\n<pre class=\"lang:python decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\"&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=ISO-8859-1\"&gt;\n&lt;title&gt;Insert title here&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\t&lt;form action=\"\/signin\" method=\"post\"&gt;\n\t\tphone:&lt;input type=\"text\" name=\"phone\"&gt;&lt;br&gt;\n\t\tvscode:&lt;input type=\"password\" name=\"vscode\"&gt;&lt;br&gt;\n\t\t&lt;input type=\"submit\" value=\"login\"&gt;\n\t&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>test.py<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"># -*- coding:utf-8 -*-\n# author: lichmama\n# email: nextgodhand@163.com\n# filename: httpd.py\nimport io\nimport os\nimport sys\nimport urllib\nfrom BaseHTTPServer import HTTPServer\nfrom SimpleHTTPServer import SimpleHTTPRequestHandler\nimport urllib2\nimport json\nclass MyRequestHandler(SimpleHTTPRequestHandler):\n    protocol_version = \"HTTP\/1.1\"\n    server_version = \"PSHS\/0.1\"\n    sys_version = \"Python\/2.7.x\"\n    def get_evns(self):\n        evns = os.environ\n        return ' \\\\n '.join([k+\"=\"+v for k,v in evns.items()])\n    def do_GET(self):\n        if self.path == \"\/\" or self.path == \"\/index\":\n            content = open(\"index.html\", \"rb\").read()\n            self.send_head(content)\n        elif self.path == \"\/evns\":\n            content = self.get_evns()\n            self.send_head(content)\n        else:\n            path = self.translate_path(self.path)\n            system=sys.platform\n            osv=os.environ\n            if osv.has_key('HOMEPATH'):\n                content = os.environ['HOMEPATH']\n            else:\n                content = os.environ['HOME']\n            self.send_head(content)\n            if os.path.exists(path):\n                extn = os.path.splitext(path)[1].lower()\n                content = open(path, \"rb\").read()\n                self.send_head(content, type=self.extensions_map[extn])\n            else:\n                content = open(\"404.html\", \"rb\").read()\n                self.send_head(content, code=404)\n        self.send_content(content)\n    def do_POST(self):\n        if self.path == \"\/signin\":\n            data = self.rfile.read(int(self.headers[\"content-length\"]))\n            data = urllib.unquote(data)\n            data = self.parse_data(data)\n            test_data = {\"username\": \"zhangyong_new\",\"userpasswd\": \"123456\"}\n            requrl = \"https:\/\/dev.honcloud.honeywell.com.cn\/dashboard\/usercentre\/login\"\n            header = {\"Content-type\": \"application\/json\"}\n            test_data_urlencode=json.dumps(test_data)\n            req = urllib2.Request(url=requrl, data=test_data_urlencode,headers=header)\n            res_data = urllib2.urlopen(req)\n            res = res_data.read()\n            print res\n            self.send_head(res)\n            self.send_content(res)\n    def parse_data(self, data):\n        ranges = {}\n        for item in data.split(\"&amp;\"):\n            k, v = item.split(\"=\")\n            ranges[k] = v\n        return ranges\n    def send_head(self, content, code=200, type=\"text\/html\"):\n        self.send_response(code)\n        self.send_header(\"Content-Type\", type)\n        self.send_header(\"Content-Length\", str(len(content)))\n        self.end_headers()\n    def send_content(self, content):\n        f = io.BytesIO()\n        f.write(content)\n        f.seek(0)\n        self.copyfile(f, self.wfile)\n        f.close()<\/code> <code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">if __name__ == \"__main__\":\n    if len(sys.argv) == 2:\n        # set the target where to mkdir, and default \"D:\/web\"\n        MyRequestHandler.target = sys.argv[1]\n    try:\n        server = HTTPServer((\"0.0.0.0\", 8282), MyRequestHandler)\n        print \"pythonic-simple-http-server started, serving at http:\/\/%s:%d\"%(server.server_address[0],server.server_address[1])\n        print server.server_address\n        server.serve_forever()\n    except KeyboardInterrupt:\n        server.socket.close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 \u6700\u8fd1\u63a5\u4e86\u4e00\u4e2a\u7279\u522b\u5947\u8469\u7684\u9700\u6c42\uff0c\u8981\u6c42\u5728\u6700\u57fa\u7840\u7684python2.7\u7684docker\u73af\u5883\u4e0a\u8fd0\u884c\u4e00\u4e2a\u5c0f\u578b\u7f51\u7ad9\uff0c\u4e0d\u80fd\u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=360"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":925,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions\/925"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}