怎么开发网站

2022-11-30 投稿:李伦圣 分享

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

2、先定义一个函数,用来向网站服务器发送请求:

def yingyong(environ, start_response):

start_response('200 OK', [('Content-Type', 'text/html')])

return [b'<h1>How Are!</h1>']

要求网站在网页上显示粗体的How Are!

3、导入wsgi模块的子模块,用来创建服务器。

from wsgiref.simple_server import make_server

4、创建服务器,IP为空,端口号为900。

a=900

httpd = make_server('', a, yingyong)

这个服务器将调用前面的函数 yingyong。

5、让服务器开始运行,并长时间运行。

httpd.serve_forever()

forever,让服务器永远运行,除非服务器被迫关闭。

服务器在哪里?就在python里面,关闭python编译器,就等于关闭了服务器。

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

7、刷新这个网页,就相当于重复访问这个网页,每访问一次(刷新一次),都会向服务器发送请求,在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_server

a=900

httpd = make_server('', a, yingyong)

httpd.serve_forever()