0

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

4
  • 2
    python -m SimpleHTTPServer 8080 - This command creates a simple web server on port 8080. Commented Aug 9, 2017 at 7:42
  • Are you sure you are asking the correct question? If you want to host the Flask application, you need a WSGI server, not a straight HTTP server. If you are wanting to create a client implemented in Python to issue GET and POST requests against the Flask application, you would want to use httplib/http.client or requests Python modules. The way you have phrased the lead in to your question is confusing. Commented Aug 9, 2017 at 8:56
  • @GrahamDumpleton I do not want to use WSGI server. I was ask to make make python server instead using curl. Commented Aug 9, 2017 at 12:09
  • The curl program is a command line client for making requests. You can't make a Python server out of curl. I suggest you go read this series of posts so you understand what different bits do. Your current descriptions of what you are wanting to do are confusing. ruslanspivak.com/lsbaws-part1 Commented Aug 9, 2017 at 21:04

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I want it like WSGI and it just opens the directory. I want it similar func like as it give output on localhost/5000
1

On python3 the command is python3 -m http.server A Google search would easily lead you to this post

Comments

0

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.

Comments

0

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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.