{"id":1774,"date":"2017-07-07T10:40:15","date_gmt":"2017-07-07T09:40:15","guid":{"rendered":"http:\/\/cdblog.cdstealer.com\/?p=1774"},"modified":"2017-07-07T11:29:01","modified_gmt":"2017-07-07T10:29:01","slug":"simple-python-http-server","status":"publish","type":"post","link":"https:\/\/cdblog.cdstealer.com\/?p=1774","title":{"rendered":"Simple Python HTTP server"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1542\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2017\/01\/python_sh-600x600-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2017\/01\/python_sh-600x600-300x300.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2017\/01\/python_sh-600x600-150x150.png 150w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2017\/01\/python_sh-600x600.png 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Knocking up a simple and quick web server can be extremely useful without the need to install and configure a full fat web server.  However, this isn't secure and will expose ALL files from the current directory executed in!<\/p>\n<p>The official doc can be found <a href=\"https:\/\/docs.python.org\/2\/library\/simplehttpserver.html\" target=\"_blank\" rel=\"noopener\">here<\/a>\u00a0(v2) and <a href=\"https:\/\/docs.python.org\/3.4\/library\/http.server.html\" target=\"_blank\" rel=\"noopener\">here<\/a>\u00a0(v3).<\/p>\n<h4>NOTE:  You will need to generate the certs for HTTPS version.<\/h4>\n<h2>Python2:<\/h2>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td bgcolor=\"#bbffbb\">\n<h1>HTTP<\/h1>\n<\/td>\n<td bgcolor=\"#bbffbb\">\n<h1>HTTPS<\/h1>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>#!\/usr\/bin\/env python2\r\n\r\nimport BaseHTTPServer, SimpleHTTPServer\r\n\r\nhttpd = BaseHTTPServer.HTTPServer(('&ltlisten IP&gt', &ltport&gt),\r\n        SimpleHTTPServer.SimpleHTTPRequestHandler)\r\n\r\nhttpd.serve_forever()<\/pre>\n<p>Or a simple one liner:<\/p>\n<pre>python -m SimpleHTTPServer<\/pre>\n<\/td>\n<td>\n<pre>#!\/usr\/bin\/env python2\r\n\r\nimport BaseHTTPServer, SimpleHTTPServer\r\nimport ssl\r\n\r\nhttpd = BaseHTTPServer.HTTPServer(('&ltlisten IP&gt', &ltport&gt),\r\n        SimpleHTTPServer.SimpleHTTPRequestHandler)\r\n\r\nhttpd.socket = ssl.wrap_socket (httpd.socket,\r\n        keyfile=\"key.pem\",\r\n        certfile='cert.pem', server_side=True)\r\n\r\nhttpd.serve_forever()<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Python3:<\/h2>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td bgcolor=\"#bbffbb\">\n<h1>HTTP<\/h1>\n<\/td>\n<td bgcolor=\"#bbffbb\">\n<h1>HTTPS<\/h1>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>#!\/usr\/bin\/env python3\r\n\r\nimport http.server, socketserver\r\n\r\nHandler = http.server.SimpleHTTPRequestHandler\r\nhttpd = socketserver.TCPServer(('&ltlisten ip&gt', &ltport&gt), Handler)\r\n\r\nhttpd.serve_forever()<\/pre>\n<p>Or a simple one liner:<\/p>\n<pre>python -m http.server &ltport&gt --bind '&ltlisten ip&gt'<\/pre>\n<\/td>\n<td>\n<pre>#!\/usr\/bin\/env python3\r\n\r\nimport http.server, socketserver\r\nimport ssl\r\n\r\nHandler = http.server.SimpleHTTPRequestHandler\r\nhttpd = socketserver.TCPServer(('&ltlisten ip&gt', &ltport&gt), Handler)\r\n\r\nhttpd.socket = ssl.wrap_socket (httpd.socket,\r\n        keyfile=\"key.pem\",\r\n        certfile='cert.pem', server_side=True)\r\n\r\nhttpd.serve_forever()<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Knocking up a simple and quick web server can be extremely useful without the need to install and configure a full fat web server. However, this isn't secure and will expose ALL files from the current directory executed in! The official doc can be found here\u00a0(v2) and here\u00a0(v3). NOTE: You will need to generate the &hellip; <a href=\"https:\/\/cdblog.cdstealer.com\/?p=1774\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Simple Python HTTP server<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[46,18,261],"class_list":["post-1774","post","type-post","status-publish","format-standard","hentry","category-gentoo","tag-http","tag-linux","tag-python"],"_links":{"self":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1774"}],"version-history":[{"count":21,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1774\/revisions"}],"predecessor-version":[{"id":1795,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1774\/revisions\/1795"}],"wp:attachment":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}