python3之http.server模块
有时候我们需要快速地搭建一个web服务,这时我们就可以使用python里面的http.server模块搭建http服务器。实现的方式有以下几种。
BaseHTTPServer模块在Python3中已被合并到http.server
1. 命令行启动
pyhton -m http.server 80 #pyhton3中启动方式,开启的端口为80
python -m SimpleHTTPServer 8080 #python2启动方式
这种是常用的方式,很简单。
2. 代码调用
from http.server import HTTPServer, BaseHTTPRequestHandlerdata = {'result': 'this is a test'}
host = ('localhost', 8888)class Resquest(BaseHTTPRequestHandler):timeout = 5server_version = "Apache" #设置服务器返回的的响应头 def do_GET(self):self.send_response(200)self.send_header("Content-type","text/html") #设置服务器响应头self.send_header("test1","This is test!") #设置服务器响应头self.end_headers()buf = '''<!DOCTYPE HTML><html><head><title>Get page</title></head><body><form action="post_page" method="post">username: <input type="text" name="username" /><br />password: <input type="text" name="password" /><br /><input type="submit" value="POST" /></form></body></html>'''self.wfile.write(buf.encode()) #里面需要传入二进制数据,用encode()函数转换为二进制数据 #设置响应body,即前端页面要展示的数据def do_POST(self):path = self.pathprint(path)#获取post提交的数据datas = self.rfile.read(int(self.headers['content-length'])) #固定格式,获取表单提交的数据#datas = urllib.unquote(datas).decode("utf-8", 'ignore')self.send_response(200)self.send_header("Content-type","text/html") #设置post时服务器的响应头self.send_header("test","This is post!")self.end_headers()html = '''<!DOCTYPE HTML><html><head><title>Post page</title> </head><body>Post Data:%s <br />Path:%s</body></html>''' %(datas,self.path)self.wfile.write(html.encode()) #提交post数据时,服务器跳转并展示的页面内容if __name__ == '__main__':server = HTTPServer(host, Resquest)print("Starting server, listen at: %s:%s" % host)server.serve_forever()
然后我们可以启动http服务
访问一下,并提交数据看一下
获取到post数据
python3之http.server模块相关推荐
- python3连接sql server数据库_Python3操作SQL Server数据库(实例讲解)
1.前言 前面学完了SQL Server的基本语法,接下来学习如何在程序中使用sql,毕竟不能在程序中使用的话,实用性就不那么大了. 2.最基本的SQL查询语句 python是使用pymssql这个模 ...
- python3的数据类型以及模块的含义
python3的数据类型以及模块的含义 购物车 转载于:https://blog.51cto.com/11834445/1884901
- python3.7导入gevent模块报错的解决方案
python3.7导入gevent模块报错的解决方案 参考文章: (1)python3.7导入gevent模块报错的解决方案 (2)http://www.cnblogs.com/eva-j/p/939 ...
- 电脑安装python3.7说缺少-centos7:python3.7 缺少_ssl模块问题
在centos7上安装python3.7,很多时候提示缺少安装_ssl模块,这导致很多依赖于ssl的模块无法正常安装,如ulib3,requests. 百度网上也提供很多方法,诸如安装liber.ss ...
- Swoole 源码分析——Server模块之初始化
前言 本节主要介绍 server 模块进行初始化的代码,关于初始化过程中,各个属性的意义,可以参考官方文档: SERVER 配置选项 关于初始化过程中,用于监听的 socket 绑定问题,可以参考: ...
- linux环境下安装python3以及安装cx_Oracle模块需要注意的事项
linux环境下安装python3以及安装cx_Oracle模块需要注意的事项 python3的安装 1. 首先安装python3之前首先要安装python所依赖的环境 yum install gcc ...
- python oserror路径not found_详谈Python3 操作系统与路径 模块(os / os.path / pathlib)
python如何判断一个目录下是否存在某个文件?如果小编突然在人群中很疯或者沉默,那时一定很难过. 1.使用os模块 用os模块中os.path.exists()方法检测是否存在test_file.t ...
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- python3.8 安装 ssl 模块 和 _ctypes 模块
文章目录 前情提要 安装 openssl-1.1.1 重新编译安装 python3.8 -rpath 编译选项介绍 python3.8 跟 python3.10 的区别 前情提要 我在之前给 pyth ...
- Python3.7中time模块的time()、perf_counter()和process_time()的区别
Python3.7中time模块的time方法.perf_counter方法和process_time方法的区别 1. time.time()方法 2. time.perf_counter()方法 3 ...
最新文章
- xml转换为mysql_如何将存储在XML文件中的数据转换为关系数据库(MySQL)?
- MySQL order by 是怎么工作的?
- linux下source命令使用详解
- JavaScript函数实现方式大总结
- P3975-[TJOI2015]弦论【SAM】
- 《Python语言程序设计》——1.3 程序设计语言
- mysql字符串等于失效_MySql整型索引和字符串索引失效或隐式转换问题
- Android开发之内容提供者ContentProvider
- html5小游戏抓包,wireshark抓包分析
- 数据库的备份与恢复技术
- Specification for the Lab VIEW Measurement File
- 数据分析指标缩写英文单词解释
- 基于matlab的眼部疲劳检测
- php绑定银行卡实现,php网站如何绑定银行卡
- 3dmax软件渲染模型导出JPG格式图片的方法
- ubuntu下载安装google浏览器
- 在瑞芯微ok3568平台利用python实现Can通讯
- see “systemctl starus network.service“and “journalctl -xe for details克隆虚拟机出来网络ping不通百度的问题解决方案及解答
- gittortoise使用ssh秘钥
- OSChina 周六乱弹 ——抗议破坏教学道具,致其严重变形!