#!/usr/bin/env python3# A simple HTTP service built directly against the low-level WSGI spec.from pprint import pformatfrom wsgiref.simple_server import make_serverdef app(environ, start_response): headers = {'Content-Type': 'text/plain; charset=utf-8'} start_response('200 OK', list(headers.items())) yield 'Here is the WSGI environment:'.encode('utf-8') yield pformat(environ).encode('utf-8')if __name__ == '__main__': httpd = make_server('', 8000, app) host, port = httpd.socket.getsockname() print('Serving on', host, 'port', port) httpd.serve_forever()
上述只是一个简单的情况。
但是在编写服务器程序时,复杂度就大大提升了。
这是因为要完全考虑标准中的描述的许多注意点和边界情况。
2、前向代理与反向代理
无论前向代理还是反向代理,HTTP代理其实就是一个HTTP服务器,用于接收请求,然后对接收到的请求(至少是部分请求)进行转发。
转发请求时代理会扮演客户端的角色,将转发的HTTP请求发送至真正的服务器,最后将从服务器接受到的响应发挥扮演客户端的角色,将转发的请求发送至真正的服务器,最后将从服务器接受到的响应发回给最初的客户端。
下面是前向代理和反向代理的简图。
#!/usr/bin/env python3# A simple HTTP service built directly against the low-level WSGI spec.import timedef app(environ, start_response): host = environ.get('HTTP_HOST', '127.0.0.1') path = environ.get('PATH_INFO', '/') if ':' in host: host, port = host.split(':', 1) if '?' in path: path, query = path.split('?', 1) headers = [('Content-Type', 'text/plain; charset=utf-8')] if environ['REQUEST_METHOD'] != 'GET': start_response('501 Not Implemented', headers) yield b'501 Not Implemented' elif host != '127.0.0.1' or path != '/': start_response('404 Not Found', headers) yield b'404 Not Found' else: start_response('200 OK', headers) yield time.ctime().encode('ascii')
第一段比较冗长。
下面使用第三方库简化原始WGSI的模式方法。
第一个示例是使用WebOb编写的可调用对象返回当前时间。
#!/usr/bin/env python3# A WSGI callable built using webob.import time, webobdef app(environ, start_response): request = webob.Request(environ) if environ['REQUEST_METHOD'] != 'GET': response = webob.Response('501 Not Implemented', status=501) elif request.domain != '127.0.0.1' or request.path != '/': response = webob.Response('404 Not Found', status=404) else: response = webob.Response(time.ctime()) return response(environ, start_response)
第二个是使用Werkzeug编写的WSGI可调用对象返回当前时间。
#!/usr/bin/env python3# A WSGI callable built using Werkzeug.import timefrom werkzeug.wrappers import Request, Response@Request.applicationdef app(request): host = request.host if ':' in host: host, port = host.split(':', 1) if request.method != 'GET': return Response('501 Not Implemented', status=501) elif host != '127.0.0.1' or request.path != '/': return Response('404 Not Found', status=404) else: return Response(time.ctime())
大家可以对比这两个库在简化操作时的不同之处,Werkzeug是Flask框架的基础。
让您学习到的每一节课都有所收获
《Linux就该这么学》是一本由资深运维专家刘遄老师及国内多名红帽架构师(RHCA)基于最新RHEL7系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材。荣获双11、双12购物狂欢节IT品类书籍销量冠军,2017年、2018年国内读者增速最快的技术书籍,您可以在京东、当当、亚马逊及天猫搜索书名后购买,亦可加刘遄老师微信交流学习(手指按住下图3秒钟即可自动扫描)~
刘遄老师QQ:5604922
☀ Linux技术交流群:2265381(新群,火热加群中……)
☀ 官方站点:www.linuxprobe.com
☀ 书籍在线学习(电脑在线阅读效果更佳):
http://www.linuxprobe.com/chapter-00.html
《Linux就该这么学》是一本基于最新Linux系统编写,面向零基础读者的技术书籍。从Linux基础知识讲起,然后渐进式地提高内容难度,详细讲解Linux系统中各种服务的工作原理和配置方式,以匹配真实生产环境对运维人员的要求,突显内容的实用性。想要学习Linux系统的读者可以点击按钮了解这本书,同时这本书也适合专业的运维人员阅读,作为一本非常有参考价值的工具书!