{"id":972,"date":"2014-03-11T18:39:23","date_gmt":"2014-03-11T18:39:23","guid":{"rendered":"https:\/\/trouble.org\/?p=972"},"modified":"2014-03-11T18:43:43","modified_gmt":"2014-03-11T18:43:43","slug":"https-server","status":"publish","type":"post","link":"https:\/\/trouble.org\/?p=972","title":{"rendered":"https server"},"content":{"rendered":"<p>In conjunction with the openssl scripts&#8230; a little bit of python code to fire up an ssl server&#8230; lots of them out there, this one is mine.<\/p>\n<div class=\"codecolorer-container python blackboard\" style=\"overflow:auto;white-space:nowrap;height:800px;\"><div class=\"python codecolorer\"><span class=\"co1\">#!\/usr\/bin\/env python<\/span><br \/>\n<br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># fire up an HTTPS\/ssl web server in the PWD (defaults to localhost:8081)<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># optional - give a file name to serve up, like &quot;$0 foofile.html&quot;<\/span><br \/>\n<span class=\"co1\"># If you use this option it'll wait a few seconds and then try to open it<\/span><br \/>\n<span class=\"co1\"># using the <\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># Keyfiles are in variables... defaulting to \/tmp + server.crt, server.key, and ca.crt.<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<br \/>\n<span class=\"kw1\">import<\/span> <span class=\"kw3\">BaseHTTPServer<\/span><span class=\"sy0\">,<\/span> <span class=\"kw3\">SimpleHTTPServer<\/span><span class=\"sy0\">,<\/span> <span class=\"kw3\">os<\/span><span class=\"sy0\">,<\/span> <span class=\"kw3\">sys<\/span><span class=\"sy0\">,<\/span> ssl<br \/>\n<br \/>\n<span class=\"co1\"># server='192.168.0.7'<\/span><br \/>\nhost &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">=<\/span> <span class=\"st0\">'127.0.0.1'<\/span><br \/>\nport &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">=<\/span> <span class=\"nu0\">8081<\/span><br \/>\n<br \/>\n<span class=\"co1\"># cert files<\/span><br \/>\ncertfile &nbsp; &nbsp; <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;\/tmp\/server.crt&quot;<\/span><br \/>\nkeyfile &nbsp; &nbsp; &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;\/tmp\/server.key&quot;<\/span><br \/>\nca_certz &nbsp; &nbsp; <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;\/tmp\/ca.crt&quot;<\/span><br \/>\n<br \/>\n<span class=\"co1\"># command to open up a URL &amp; the server<\/span><br \/>\nfetch_url &nbsp; &nbsp;<span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;open&quot;<\/span><br \/>\nserver &nbsp; &nbsp; &nbsp; <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;https:\/\/%s:%s\/&quot;<\/span> % <span class=\"br0\">&#40;<\/span>host<span class=\"sy0\">,<\/span> port<span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"co1\"># actual web server stuff<\/span><br \/>\nhttpd &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"sy0\">=<\/span> <span class=\"kw3\">BaseHTTPServer<\/span>.<span class=\"me1\">HTTPServer<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#40;<\/span>host<span class=\"sy0\">,<\/span> port<span class=\"br0\">&#41;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw3\">SimpleHTTPServer<\/span>.<span class=\"me1\">SimpleHTTPRequestHandler<\/span><span class=\"br0\">&#41;<\/span><br \/>\nhttpd.<span class=\"kw3\">socket<\/span> <span class=\"sy0\">=<\/span> ssl.<span class=\"me1\">wrap_socket<\/span> <span class=\"br0\">&#40;<\/span>httpd.<span class=\"kw3\">socket<\/span><span class=\"sy0\">,<\/span> ca_certs<span class=\"sy0\">=<\/span>ca_certz<span class=\"sy0\">,<\/span> certfile<span class=\"sy0\">=<\/span>certfile<span class=\"sy0\">,<\/span> keyfile<span class=\"sy0\">=<\/span>keyfile<span class=\"sy0\">,<\/span> server_side<span class=\"sy0\">=<\/span><span class=\"kw2\">True<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">try<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"co1\"># fire up a browser to fetch the url, but sleep a few seconds to give the server a chance to get going<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"kw2\">len<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw3\">sys<\/span>.<span class=\"me1\">argv<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">&gt;<\/span> <span class=\"nu0\">1<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; shell_string <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;(sleep 3; %s %s\/%s) &amp;&quot;<\/span> % <span class=\"br0\">&#40;<\/span>fetch_url<span class=\"sy0\">,<\/span> server<span class=\"sy0\">,<\/span> <span class=\"kw3\">sys<\/span>.<span class=\"me1\">argv<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">try<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">os<\/span>.<span class=\"me1\">system<\/span><span class=\"br0\">&#40;<\/span>shell_string<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">except<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;'%s' not supported on this OS ('%s' works on mac)&quot;<\/span> % <span class=\"br0\">&#40;<\/span>fetch_url<span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;open&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">print<\/span> <span class=\"st0\">&quot;Serving SSL webz @ %s&quot;<\/span> % server<br \/>\n&nbsp; &nbsp; httpd.<span class=\"me1\">serve_forever<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">except<\/span> <span class=\"kw2\">Exception<\/span><span class=\"sy0\">,<\/span> err:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">print<\/span> <span class=\"st0\">&quot;couldn't start web server: %s&quot;<\/span> % err<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In conjunction with the openssl scripts&#8230; a little bit of python code to fire up an ssl server&#8230; lots of them out there, this one is mine. #!\/usr\/bin\/env python # # fire up an HTTPS\/ssl web server in the PWD (defaults to localhost:8081) # # optional &#8211; give a file name to serve up, like [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,260,172,4],"tags":[266,261,130],"class_list":["post-972","post","type-post","status-publish","format-standard","hentry","category-code","category-crypto","category-python","category-security","tag-https","tag-openssl","tag-ssl"],"_links":{"self":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=972"}],"version-history":[{"count":2,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/972\/revisions"}],"predecessor-version":[{"id":975,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/972\/revisions\/975"}],"wp:attachment":[{"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}