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/) ...
相关推荐
牵牛老人4 分钟前
Qt技巧(三)编辑框嵌入按钮,系统位数判断,判断某对象是否属于某种类,控件取句柄,支持4K,巧用QEventLoop,QWidget的窗体样式
开发语言·qt
Niu_brave8 分钟前
Python基础知识学习(2)
开发语言·python·学习
geekrabbit15 分钟前
Ubuntu 22.04上安装Python 3.10.x
linux·python·ubuntu
神仙别闹24 分钟前
基于C#+Mysql实现(界面)企业的设备管理系统
开发语言·mysql·c#
deflag28 分钟前
第T1周:Tensorflow实现mnist手写数字识别
人工智能·python·机器学习·分类·tensorflow
大柏怎么被偷了31 分钟前
【C++算法】位运算
开发语言·c++·算法
程序猿方梓燚33 分钟前
C/C++实现植物大战僵尸(PVZ)(打地鼠版)
c语言·开发语言·c++·算法·游戏
CPP_ZhouXuyang33 分钟前
C语言——模拟实现strcpy
c语言·开发语言·数据结构·算法·程序员创富
Zucker n39 分钟前
猫狗识别大模型——基于python语言
开发语言·python
bluebonnet271 小时前
【Rust练习】15.match 和 if let
开发语言·后端·rust