Reputation: 13
I have designed a rest API with Flask now I want to create a simple web server in python to get and post data. How to create web server in python? I do not want to use curl and localserver 5000
Upvotes: 0
Views: 5175
Reputation: 660
For really simple options, as per the one-liners given. For something that can expand easily in the asyncio framework, this is not a bad start to serve files from the current folder (hence the os.path).
import asyncio
from aiohttp import web
from os.path import abspath, dirname, join
async def main():
app = web.Application()
app.add_routes([
web.static('/', abspath(join(dirname(__file__))))
])
runner = web.AppRunner(app)
await runner.setup()
await web.TCPSite(runner, 'localhost', '8080').start()
await asyncio.get_running_loop().create_future()
asyncio.run(main())
Upvotes: 0
Reputation: 1146
On python3 the command is python3 -m http.server
A Google search would easily lead you to this post
Upvotes: 1
Reputation: 2652
For Linux Open up a terminal and type:
$ cd /home/somedir
$ python -m SimpleHTTPServer
Now your http server will start in port 8000
. You will get the message:
Serving HTTP on 0.0.0.0 port 8000 ...
Now open a browser and type the following address:
http://your_ip_address:8000
You can also access it via:
http://127.0.0.1:8000
or
http://localhost:8000
Also note:
If the directory has a file named index.html, that file will be served as the initial file. If there is no index.html, then the files in the directory will be listed.
If you wish to change the port that's used start the program via:
$ python -m SimpleHTTPServer 8080
change the port number to anything you want.
Upvotes: 2
Reputation: 2065
To create a simple HTTP webserver in python, use the in-built SimpleHTTPServer module as shown below:
python -m SimpleHTTPServer 8080
where 8080 is the port number.
Upvotes: 0