Basic Web Server (On the fly Creation)
Hello World,
Many times specially during web development on core HTML 5[although its not just limited here only.], we need a web server to test the application and specially JavaScript and other features of html 5, which are only available on localhost,HTTP.
Now, here you can go with traditional web servers like Tomcat Apache or any other server, but then you need to configure them accordingly and very carefully and that may took very dedicated time around 00:30 mins to 1 hour [i am talking here about very basic web server].
So, is there any techniques that can help us to creating a web server in just a simple one line command hit... Yes there is. actually there are many server framework that are just a few line setup away to have a basic web server.
Here i gonna talk very simplest one, that will help us to create a very basic web server. And it is Python's module "SimpleHTTPServer".
Dependency/Requirement: But there is a very small dependency to have you on your system where you want your server. and its PYTHON-2.7 that's it, nothing else.
if you don't have python-2.7 installed on your system, then just go here and install the python-2.7 right now, and if you have already having python-2.7 installed on your system, then just hit the following command
Command / Demo:
Syntex (for python2): >>> python -m SimpleHTTPServer
Syntex (for python3): >>> python -m http.server
E.g.(Python2): >>> python -m SimpleHTTPServer 8900
E.g.(Python3): >>> python -m http.server 8900
Serving HTTP on 0.0.0.0 port 8900 ...
Things you have to Notice here are:
1# The location of current working directory would be public after hitting this command. So just by changing the current directory location you can easily change the public folder to be served by this HTTP sever.
2# PORT_NUMBER. [0-1023] are system reserved ports. So don's use these ports. you may use port number greater than 1024 and less than 65565, if that port would be available then it would start a HTTP server on that port.
Many times specially during web development on core HTML 5[although its not just limited here only.], we need a web server to test the application and specially JavaScript and other features of html 5, which are only available on localhost,HTTP.
Now, here you can go with traditional web servers like Tomcat Apache or any other server, but then you need to configure them accordingly and very carefully and that may took very dedicated time around 00:30 mins to 1 hour [i am talking here about very basic web server].
So, is there any techniques that can help us to creating a web server in just a simple one line command hit... Yes there is. actually there are many server framework that are just a few line setup away to have a basic web server.
Here i gonna talk very simplest one, that will help us to create a very basic web server. And it is Python's module "SimpleHTTPServer".
Dependency/Requirement: But there is a very small dependency to have you on your system where you want your server. and its PYTHON-2.7 that's it, nothing else.
if you don't have python-2.7 installed on your system, then just go here and install the python-2.7 right now, and if you have already having python-2.7 installed on your system, then just hit the following command
Command / Demo:
Syntex (for python2): >>> python -m SimpleHTTPServer
Syntex (for python3): >>> python -m http.server
E.g.(Python2): >>> python -m SimpleHTTPServer 8900
E.g.(Python3): >>> python -m http.server 8900
Serving HTTP on 0.0.0.0 port 8900 ...
The reason, its showing the directory listing because we do not have any index.htm/l file here. Just simply put your index.htm/l file to start serving your web application directly without showing the listing.
So from here, one thing is also concluded that you can share any file[txt, json, media, etc] with public.
Things you have to Notice here are:
1# The location of current working directory would be public after hitting this command. So just by changing the current directory location you can easily change the public folder to be served by this HTTP sever.
2# PORT_NUMBER. [0-1023] are system reserved ports. So don's use these ports. you may use port number greater than 1024 and less than 65565, if that port would be available then it would start a HTTP server on that port.
Comments
Post a Comment