Python HTTP Server

目录

  • [1. Python HTTP Server](#1. Python HTTP Server)
    • [1.1. Python 内置](#1.1. Python 内置)

1. Python HTTP Server

1.1. Python 内置

Python 2:

sh 复制代码
$ python -m SimpleHTTPServer 9000

If you are running Python 3, you will get error as No module named SimpleHTTPServer. It's because in python 3, SimpleHTTPServer has been merged into http.server module. You can use below command to run python http server in Python 3.

sh 复制代码
$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

To explicitly set the port number that your HTTP server should be listening on, append it as a parameter:

sh 复制代码
$ python3 -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...

Port 80 is a standard port reserved for HTTP traffic. However, if you'd like to start a local web server on that special port, then you'll have to run the corresponding command as the superuser with administrative privileges. Otherwise, you'll get another error:

sh 复制代码
$ python3 -m http.server 80
Traceback (most recent call last):
  ...
PermissionError: [Errno 13] Permission denied

You can bind a specific network interface or IP address by using the -b option:

sh 复制代码
$ python3 -m http.server -b 127.0.0.42 8080
Serving HTTP on 127.0.0.42 port 8080 (http://127.0.0.42:8080/) ...

To enforce a different address, you can use the -b option:

sh 复制代码
$ python -m http.server -b "::"
Serving HTTP on :: port 8000 (http://[::]:8000/) ...

You may instruct the server to associate its home address (/) with a completely different directory by specifying the optional -d parameter:

sh 复制代码
$ python3 -m http.server -d ~/Pictures/
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

The -d option may be your only choice in some cases. For instance, if you try to start the HTTP server in a directory where you installed your Python interpreter, then you may face the following problem:

sh 复制代码
$ cd /usr/lib/python3.8/
$ python3 -m http.server
Traceback (most recent call last):
  ...
AssertionError: SRE module mismatch

You can work around this issue by changing your working directory so that Python will no longer find this module in the file system. Then, start the server using the -d option to point it back to the desired directory:

sh 复制代码
$ cd ..
$ python3 -m http.server -d /usr/lib/python3.8/
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
相关推荐
时光の尘几秒前
C语言菜鸟入门·关键字·float以及double的用法
运维·服务器·c语言·开发语言·stm32·单片机·c
以后不吃煲仔饭14 分钟前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师15 分钟前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言
前端拾光者19 分钟前
利用D3.js实现数据可视化的简单示例
开发语言·javascript·信息可视化
程序猿阿伟21 分钟前
《C++ 实现区块链:区块时间戳的存储与验证机制解析》
开发语言·c++·区块链
傻啦嘿哟39 分钟前
如何使用 Python 开发一个简单的文本数据转换为 Excel 工具
开发语言·python·excel
大数据编程之光43 分钟前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
初九之潜龙勿用43 分钟前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net
B站计算机毕业设计超人1 小时前
计算机毕业设计SparkStreaming+Kafka旅游推荐系统 旅游景点客流量预测 旅游可视化 旅游大数据 Hive数据仓库 机器学习 深度学习
大数据·数据仓库·hadoop·python·kafka·课程设计·数据可视化
Dola_Pan1 小时前
C语言:数组转换指针的时机
c语言·开发语言·算法