如何在 python 中创建一个简单的 HTTP 网络服务器? [英] How to create a simple HTTP webserver in python?

查看:36
本文介绍了如何在 python 中创建一个简单的 HTTP 网络服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 Flask 设计了一个 rest API,现在我想在 python 中创建一个简单的 Web 服务器来获取和发布数据.如何在python中创建Web服务器?我不想使用 curl 和 localserver 5000

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

推荐答案

For Linux打开终端并输入:

For Linux Open up a terminal and type:

$ cd /home/somedir
$ python -m SimpleHTTPServer

现在您的 http 服务器将在端口 8000 中启动.您将收到消息:

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

http://localhost:8000

另请注意:

如果目录有一个名为 index.html 的文件,该文件将作为初始文件.如果没有index.html,则列出目录下的文件.

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.

这篇关于如何在 python 中创建一个简单的 HTTP 网络服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆