手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

(web开发)用python创建一个最简单的网站

时间:2024-10-15 05:39:58

1、开始之前,请先用浏览器访问下面这个网页:http://127.0.0.1:900/

(web开发)用python创建一个最简单的网站

2、先定义一个函数,用来向网站服务器发送请求:def 鲻戟缒男yingyong(environ, start_response): start_respons髫潋啜缅e('200 OK', [('Content-Type', 'text/html')]) return [b'<h1>How Are!</h1>']要求网站在网页上显示粗体的How Are!

(web开发)用python创建一个最简单的网站

3、导入wsgi模块的子模块,用来创建服务器。from wsgiref.simple_server import make_server

(web开发)用python创建一个最简单的网站

4、创建服务器,IP为空,端口号为900。a=900httpd = make_server('', a, yingyong)这个服务器将调用前面的函数yingyong。

(web开发)用python创建一个最简单的网站

5、让服务器开始运行,并长时间运行。httpd.serve_forever()forever,让服务器永远运行,坛冰核哧除非服务器被迫关闭。服务器在哪里?就在python里面,关闭python编译器,就等于关闭了服务器。

(web开发)用python创建一个最简单的网站

6、再访问步骤一里面的链接,就得到如下网页。这说明服务器开始运行了。

(web开发)用python创建一个最简单的网站

7、刷新这个网页,就相当于重复访问这个网页。每访问一次(刷新一次),都会向服务器发送请求,在python编译器里面会有所体现。

(web开发)用python创建一个最简单的网站

8、关闭python编译器,服务器也就关闭了,这个网页会立刻崩溃。再打开python并运行这段代码,这个网页又会立刻恢复。完整沪枭诽纾代码如下:def yingyong(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return [b'<h1>How Are!</h1>']from wsgiref.simple_server import make_servera=900httpd = make_server('', a, yingyong)httpd.serve_forever()

(web开发)用python创建一个最简单的网站
© 手抄报圈