How to Create Python Server (Simple Steps) | Run Web Story using Python Server | Create local HTTP Python server
Ans. Python is a programming language which creates a Server using python script making your computer as webhost to host HTML files just like other webhosts or ftp clients.
Python will access your given folder as a web directory to access and run HTML, JavaScript & CSS files.
Q. So, How does it work?
Ans. Steps for follow:-
1. Create a folder name it PUBLIC. (You can name whatever you like)
2. Create a file and name it as SERVER.py
3. Copy the Code given below in the SERVER.py and save it inside PUBLIC folder.
4. Create a html file name it index.html
5. Edit index.html and copy paste this html code <html>hello world</html>
6. Save it & Run the Python Script and minimize it.
7. Open your browser Chrome | Mozilla and type this url 127.0.0.1:8000
9. You will see your index.html is working correctly.
Points:-
- 8000 is your port number as mentioned in the python script
- Add your own html code and see how it works.
- It will only Run - HTML, JAVASCRIPT, CSS
- It will not run - PHP
Very handy while making Web Stories
import webbrowser # Checking Python Version Compatibility try: # If Python 2.7+v installed Run this import SimpleHTTPServer as svr import SocketServer as soc except: # If Python 3.0+v installed Run this import http.server as svr import socketserver as soc PORT = 8000 Handler = svr.SimpleHTTPRequestHandler http_public = soc.TCPServer(("", PORT), Handler) print ("serving at port \t > \t", PORT) print ("Open Browser and Run \t > \t 127.0.0.1:8000") # this will open your index.html page. # Refresh browser if showing [try again] webbrowser.open('http://127.0.0.1:8000') http_public.serve_forever() #------------------------- # To Run in command line: # for Python 2.7 # python -m SimpleHTTPServer 8000 # for Python 3.7 # python -m http.server 8000 #------------------------- # Your URL type 127.0.0.1:8000 # Html should be index.html or index.htm # Else you will see only list of files in that folder.
*First Download Web Story Sample from here
*Source:- https://amp.dev/documentation/guides-and-tutorials/start/visual_story/setting_up/
*Extract the contents of the zip file in a folder and name the folder "PUBLIC"
*Copy the Python Script in the same folder and name it SERVER.py and RUN
*View the Effect by clicking on the html pages.
Comments
Post a Comment