{"id":13804,"date":"2019-04-06T09:19:01","date_gmt":"2019-04-06T09:19:01","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13804"},"modified":"2019-04-06T09:19:01","modified_gmt":"2019-04-06T09:19:01","slug":"how-to-use-python-simplehttpserver-to-create-webserver-or-serve-files-instantly","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-use-python-simplehttpserver-to-create-webserver-or-serve-files-instantly\/","title":{"rendered":"How to Use Python \u2018SimpleHTTPServer\u2019 to Create Webserver or Serve Files Instantly"},"content":{"rendered":"<p><strong>SimpleHTTPServer<\/strong>\u00a0is a python module which allows you to instantly create a web server or serve your files in a snap. Main advantage of python\u2019s SimpleHTTPServer is you don\u2019t need to install anything since you have python interpreter installed. You don\u2019t have to worry about python interpreter because almost all Linux distributions, python interpreter come handy by default.<\/p>\n<p>You also can use SimpleHTTPServer as a file sharing method. You just have to enable the module within the location of your shareable files are located. I will show you several demonstrations in this article by using various options.<\/p>\n<h3>Step 1: Check for Python Installation<\/h3>\n<p><strong>1.<\/strong>\u00a0Check whether python is installed in your server or not, by issuing below command.<\/p>\n<pre># python \u2013V \r\n\r\nOR\r\n\r\n# python  --version\r\n<\/pre>\n<p>It will show you the version of the python interpreter you\u2019ve got and it will give you an error message if it is not installed.<\/p>\n<div id=\"attachment_12248\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Check-Python.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12248\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Check-Python.jpg\" alt=\"Check Python Version\" width=\"302\" height=\"139\" aria-describedby=\"caption-attachment-12248\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12248\" class=\"wp-caption-text\">Check Python Version<\/p>\n<\/div>\n<p><strong>2.\u00a0<\/strong>You\u2019re lucky if it was there by default. Less work actually. If it was not installed by any chance, install it following below commands.<\/p>\n<p>If you have a\u00a0<strong>SUSE<\/strong>\u00a0distribution, type\u00a0<code>yast<\/code>\u00a0in the terminal\u00a0<strong>\u2013&gt;<\/strong>\u00a0Go to Software Management\u00a0<strong>\u2013&gt;<\/strong>\u00a0Type\u00a0<code>\u2018python\u2019<\/code>without quotes\u00a0<strong>\u2013&gt;<\/strong>\u00a0select python interpreter\u00a0<strong>\u2013&gt;<\/strong>\u00a0press space key and select it\u00a0<strong>\u2013&gt;<\/strong>\u00a0and then install it.<\/p>\n<p>Simple as that. For that, you need to have SUSE ISO mounted and configured it as a repo by\u00a0<strong>YaST<\/strong>\u00a0or you can simple install python from the web.<\/p>\n<div id=\"attachment_12249\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Install-Python-on-Suse.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-12249\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Install-Python-on-Suse-620x164.jpg\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Install-Python-on-Suse-620x164.jpg 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Install-Python-on-Suse-1024x271.jpg 1024w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Install-Python-on-Suse.jpg 1263w\" alt=\"Install Python on Suse\" width=\"620\" height=\"164\" aria-describedby=\"caption-attachment-12249\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12249\" class=\"wp-caption-text\">Install Python on Suse<\/p>\n<\/div>\n<p>If you\u2019re using different operating systems like RHEL, CentOS, Debian, Ubuntu or other Linux operating systems, you can just install python using yum or apt.<\/p>\n<p>In my case I use\u00a0<strong>SLES 11 SP3 OS<\/strong>\u00a0and python interpreter comes installed by default in it. Most of the case you won\u2019t have to worry about installing python interpreter on your server.<\/p>\n<h3>Step 2: Create a Test Directory and Enable SimpleHTTPServer<\/h3>\n<p><strong>3.<\/strong>\u00a0Create a test directory where you don\u2019t mess with system files. In my case I have a partition called\u00a0<code>\/x01<\/code>\u00a0and I have created a directory called\u00a0<code>tecmint<\/code>\u00a0in there and also I have added some test files for testing.<\/p>\n<div id=\"attachment_12250\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Create-Testing-Directory.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12250\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Create-Testing-Directory.jpg\" alt=\"Create Testing Directory\" width=\"408\" height=\"262\" aria-describedby=\"caption-attachment-12250\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12250\" class=\"wp-caption-text\">Create Testing Directory<\/p>\n<\/div>\n<p><strong>4.<\/strong>\u00a0Your prerequisites are ready now. All you have to do is try python\u2019s\u00a0<strong>SimpleHTTPServer<\/strong>\u00a0module by issuing below command within your test directory (In my case,\u00a0<strong>\/x01\/<\/strong>\/).<\/p>\n<pre># python \u2013m SimpleHTTPServer\r\n<\/pre>\n<div id=\"attachment_12251\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Enable-SimpleHTTPServer.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12251\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Enable-SimpleHTTPServer.jpg\" alt=\"Enable SimpleHTTPServer\" width=\"376\" height=\"162\" aria-describedby=\"caption-attachment-12251\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12251\" class=\"wp-caption-text\">Enable SimpleHTTPServer<\/p>\n<\/div>\n<p><strong>5.<\/strong>\u00a0After enabling\u00a0<strong>SimpleHTTPServer<\/strong>\u00a0successfully, it will start serving files through port number\u00a0<strong>8000<\/strong>. You just have to open up a web browser and\u00a0<strong>enter ip_address:port_number<\/strong>\u00a0(in my case its\u00a0<strong>192.168.5.67:8000<\/strong>).<\/p>\n<div id=\"attachment_12252\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/SimpleHTTPServer-Directory-Listing.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12252\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/SimpleHTTPServer-Directory-Listing.jpg\" alt=\"SimpleHTTPServer Directory Listing\" width=\"425\" height=\"355\" aria-describedby=\"caption-attachment-12252\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12252\" class=\"wp-caption-text\">Directory Listing<\/p>\n<\/div>\n<p><strong>6.<\/strong>\u00a0Now click on link\u00a0<code>'tecmint'<\/code>\u00a0to browse files and directories of\u00a0<strong>tecmint<\/strong>\u00a0directory, see the screen below for reference.<\/p>\n<div id=\"attachment_12253\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Browse-Directory-Files.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12253\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Browse-Directory-Files.jpg\" alt=\"Browse Directory Files\" width=\"353\" height=\"317\" aria-describedby=\"caption-attachment-12253\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12253\" class=\"wp-caption-text\">Browse Directory Files<\/p>\n<\/div>\n<p><strong>7.<\/strong>\u00a0<strong>SimpleHTTPServer<\/strong>\u00a0serves your files successfully. You can see what has happened at the terminal, after you accessed your server through web browser by having a look at where you executed your command.<\/p>\n<div id=\"attachment_12254\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Status-of-Command.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12254\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Status-of-Command.jpg\" alt=\"Python SimpleHTTPServer Status\" width=\"520\" height=\"238\" aria-describedby=\"caption-attachment-12254\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12254\" class=\"wp-caption-text\">Python SimpleHTTPServer Status<\/p>\n<\/div>\n<h3>Step 3: Changing SimpleHTTPServer Port<\/h3>\n<p><strong>8.<\/strong>\u00a0By default python\u2019s SimpleHTTPServer serves files and directories through port\u00a0<strong>8000<\/strong>, but you can define a different port number (Here I am using port\u00a0<strong>9999<\/strong>) as you desire with the python command as shown below.<\/p>\n<pre># python \u2013m SimpleHTTPServer 9999\r\n<\/pre>\n<div id=\"attachment_12255\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Change-SimpleHTTPServer-Port.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12255\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Change-SimpleHTTPServer-Port.jpg\" alt=\"Change SimpleHTTPServer Port\" width=\"578\" height=\"226\" aria-describedby=\"caption-attachment-12255\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12255\" class=\"wp-caption-text\">Change SimpleHTTPServer Port<\/p>\n<\/div>\n<div id=\"attachment_12256\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Directory-Listing.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12256\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Directory-Listing.jpg\" alt=\"Directory Listing on Different Port\" width=\"362\" height=\"292\" aria-describedby=\"caption-attachment-12256\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12256\" class=\"wp-caption-text\">Directory Listing on Different Port<\/p>\n<\/div>\n<h3>Step 4: Serve Files from Different Location<\/h3>\n<p><strong>9.<\/strong>\u00a0Now as you tried it, you might like to serve your files in a specific location without actually going to the path.<\/p>\n<p>As an example, if you are in your home directory and you want to server your files in\u00a0<strong>\/x01\/tecmint\/<\/strong>\u00a0directory without\u00a0<strong>cd<\/strong>\u00a0in to\u00a0<strong>\/x01\/tecmint<\/strong>, Let\u2019s see, how we will do this.<\/p>\n<pre># pushd \/x01\/tecmint\/; python \u2013m SimpleHTTPServer 9999; popd;\r\n<\/pre>\n<div id=\"attachment_12257\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Server-Files-from-Location.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12257\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Server-Files-from-Location.jpg\" alt=\"Serve Files from Location\" width=\"573\" height=\"217\" aria-describedby=\"caption-attachment-12257\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12257\" class=\"wp-caption-text\">Serve Files from Location<\/p>\n<\/div>\n<div id=\"attachment_12256\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Directory-Listing.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12256\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Directory-Listing.jpg\" alt=\"Directory Listing on Different Port\" width=\"362\" height=\"292\" aria-describedby=\"caption-attachment-12256\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12256\" class=\"wp-caption-text\">Directory Listing on Different Port<\/p>\n<\/div>\n<h3>Step 5: Serve HTML Files<\/h3>\n<p><strong>10.<\/strong>\u00a0If there\u2019s a\u00a0<code>index.html<\/code>\u00a0file located in your serving location, python interpreter will automatically detect it and serve the html file instead of serving your files.<\/p>\n<p>Let\u2019s have a look at it. In my case I include a simple html script in the file named\u00a0<code>index.html<\/code>\u00a0and locate it in\u00a0<strong>\/x01\/tecmint\/<\/strong>.<\/p>\n<pre>&lt;html&gt;\r\n&lt;header&gt;&lt;title&gt;TECMINT&lt;\/title&gt;&lt;\/header&gt;\r\n&lt;body text=\"blue\"&gt;&lt;H1&gt;\r\nHi all. SimpleHTTPServer works fine.\r\n&lt;\/H1&gt;\r\n&lt;p&gt;&lt;a href=\"https:\/\/www.tecmint.com\"&gt;Visit TECMINT&lt;\/a&gt;&lt;\/p&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<div id=\"attachment_12259\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Create-Index-File.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12259\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Create-Index-File.jpg\" alt=\"Create Index File\" width=\"430\" height=\"317\" aria-describedby=\"caption-attachment-12259\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12259\" class=\"wp-caption-text\">Create Index File<\/p>\n<\/div>\n<p>Now save it and run SimpleHTTPServer on\u00a0<strong>\/x01\/tecmint<\/strong>\u00a0and go to the location from a web browser.<\/p>\n<pre># pushd \/x01\/tecmint\/; python \u2013m SimpleHTTPServer 9999; popd;\r\n<\/pre>\n<div id=\"attachment_12260\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Enable-Index-Page.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12260\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Enable-Index-Page.jpg\" alt=\"Enable Index Page\" width=\"540\" height=\"252\" aria-describedby=\"caption-attachment-12260\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12260\" class=\"wp-caption-text\">Enable Index Page<\/p>\n<\/div>\n<div id=\"attachment_12261\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Serving-Index-Page.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-12261\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/03\/Serving-Index-Page.jpg\" alt=\"Serving Index Page\" width=\"590\" height=\"401\" aria-describedby=\"caption-attachment-12261\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-12261\" class=\"wp-caption-text\">Serving Index Page<\/p>\n<\/div>\n<p>Very simple and handy. You can serve your files or your own html code in a snap. Best thing is you won\u2019t have to worry about installing anything at all. In a scenario like you want to share a file with someone, you don\u2019t have to copy the file to a shared location or making your directories shareable.<\/p>\n<p>Just run\u00a0<strong>SimpleHTTPServer<\/strong>\u00a0on it and it is done. There is a few things you have to keep in mind when using this python module. When it serves files it runs on the terminal and prints out what happens in there. When you\u2019re accessing it from the browser or download a file from it, it shows IP address accessed it and file downloaded etc. Very handy isn\u2019t it?<\/p>\n<p>If you want to stop serving, you will have to stop the running module by pressing\u00a0<strong>ctrl+c<\/strong>. So now you know how to use python\u2019s SimpleHTTPServer module as a quick solution to serve your files. Commenting below for the suggestions and new findings would be a great favour to enhance future articles and learn new things.<\/p>\n<h3>Reference Links<\/h3>\n<p><a href=\"https:\/\/docs.python.org\/2\/library\/simplehttpserver.html\" target=\"_blank\" rel=\"noopener\">SimpleHTTPServer Docs<\/a><\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/python-simplehttpserver-to-create-webserver-or-serve-files-instantly\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SimpleHTTPServer\u00a0is a python module which allows you to instantly create a web server or serve your files in a snap. Main advantage of python\u2019s SimpleHTTPServer is you don\u2019t need to install anything since you have python interpreter installed. You don\u2019t have to worry about python interpreter because almost all Linux distributions, python interpreter come handy &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-use-python-simplehttpserver-to-create-webserver-or-serve-files-instantly\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Use Python \u2018SimpleHTTPServer\u2019 to Create Webserver or Serve Files Instantly&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13804","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13804"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13804\/revisions"}],"predecessor-version":[{"id":13805,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13804\/revisions\/13805"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}