当前位置: 首页 > news >正文

Python HTTP Server

目录

  • 1. Python HTTP Server
    • 1.1. Python 内置

1. Python HTTP Server

1.1. Python 内置

Python 2:

$ 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.

$ 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:

$ 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:

$ 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:

$ 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:

$ 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:

$ 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:

$ 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:

$ 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/) ...

相关文章:

  • 算法训练第五十九天
  • CSS:实现文字溢出显示省略号且悬浮显示tooltip完整信息
  • vue-elementPlus自动按需导入和主题定制
  • 【深度学习】 Python 和 NumPy 系列教程(一):Python基本数据类型:1、数字(整数、浮点数)及相关运算;2、布尔值
  • 将函数实现放到CPP报“无法解析的外部符号...”,系VS Bug
  • Flink从入门到精通之-05 DataStream API
  • 命名空间 namespace
  • 前端开发——Javascript的定义函数、事件处理、Math对象、Date对象
  • GPT-4老板:AI可能会杀死人类,已经出现我们无法解释的推理能力
  • CSS(三)
  • 华为nat配置实验:内网能够访问外网,内网服务器80端口映射出去
  • Linux使用:环境变量指南和CPU和GPU利用情况查看
  • 《C++那些事》Step By Step上手学习
  • CentOS8提高篇22:制作crontab计划任务,at一次性计划任务
  • Java云电子病历系统源码,提供电子病历在线制作、管理和使用的一体化电子病历
  • 【ansible】实施任务控制
  • 【愚人节专场】Java实现定时发送小情话
  • 基于SpringBoot实现CSGO游戏赛事管理系统演示【附项目源码】分享
  • 一个年薪40w软件测试员的职业规划,写给还在迷茫中的朋友
  • 使用Xarray解码GFS气象源文件和pip国内源
  • 从“龙队”到“龙副主席”,国乒这批退役球员为何不当教练了
  • 陈冬评价神二十乘组:合,三头六臂;分,独当一面
  • 涡虫首上太空,神舟二十号任务将开展3项生命科学实验
  • 特朗普:无意解雇鲍威尔,但美联储应该降低利率
  • 讲武谈兵|英国公布六代机最新渲染图,但研发面临多重难题
  • 用了半年的洗衣机竟比马桶还脏,别再这样洗衣服了