python-实现动态web服务器


#
encoding=utf-8 import socket from multiprocessing import Process import re import sys# 设置静态文件根目录 HTML_ROOT_DIR = './html'WSGI_PYTHON_DIR = './wsgipython'class HTTPServer(object):def __init__(self, application):self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)self.app = applicationdef start(self):self.server_socket.listen(128)while True:client_socket, client_address = self.server_socket.accept()# print("[%s, %s]用户连接上了" % (client_address[0], client_address[1]))print("[%s, %s]用户连接上了" % client_address)handle_client_process = Process(target=self.handle_socket, args=(client_socket,))handle_client_process.start()client_socket.close()def start_response(self, status, headers):"""status="200 ok"headers = [('Content-Type', 'text/plain')]:param status::param headers::return:"""response_headers = "HTTP1.1 " + status + "\r\n"for header in headers:response_headers += "%s: %s\r\n" % headerself.response_headers = response_headersdef handle_socket(self, client_socket):"""处理客户端请求"""# 获取客户端请求数据request_data = client_socket.recv(1024)print("request data: ", request_data)request_lines = request_data.splitlines()for request_line in request_lines:print(request_line)# 解析请求报文# 'GET / HTTP/1.1'request_start_line = request_lines[0]print('&' * 20)print(type(request_start_line))# 提取用户请求的文件名print('*' * 10)print(request_start_line.decode('utf-8'))file_name = re.match(r"\w+ +(/[^ ]*) ", request_start_line.decode('utf-8')).group(1)method = re.match(r"(\w+) +/[^ ]* ", request_start_line.decode('utf-8')).group(1)print(file_name)
# 将路由的分发交给框架去做 ====================================================
# # "/ctime.py"# # "/sayhello.py"# if file_name.endswith('.py'):# a = file_name[1:-3]# try:# m = __import__(file_name[1:-3])# except Exception:# self.response_headers = "HTTP/1/1 404 Not Found\r\n"# response_body = 'not found'# else:# env = {# "PATH_INFO": file_name,# 'METHOD': method# }# # response_body = m.application(env, self.start_response)# # response = self.response_headers + '\r\n' + response_body# # else:# if '/' == file_name:# file_name = '/index.html'# # # 打开文件,读取内容# try:# file = open(HTML_ROOT_DIR + file_name, "rb")# except IOError:# response_start_line = 'HTTP/1.1 404 Not Found\r\n'# response_headers = 'Server: Myserver\r\n'# response_body = 'The file is not found!'# else:# file_data = file.read()# file.close()# # #  构造响应数据# response_start_line = 'HTTP/1.1 200 OK\r\n'# response_headers = 'Server: Myserver\r\n'# response_body = file_data.decode('utf-8')# print('??' * 20)# print(type(response_body))# # response = response_start_line + response_headers + '\r\n' + response_body# print('response data:', response) env = {"PATH_INFO": file_name,'METHOD': method}response_body = self.app(env, self.start_response)response = self.response_headers + '\r\n' + response_body# 向客户端返回响应数据client_socket.send(bytes(response, 'utf-8'))# 关闭客户端连接 client_socket.close()def bind(self, port):self.server_socket.bind(("", port))def main():sys.path.insert(1, WSGI_PYTHON_DIR)if len(sys.argv) < 2:sys.exit("python MyWebServer_v1.py Module:app")# python MyWebServer_v1.py MyWebFrameWork:appmodule_name, app_name = sys.argv[1].split(":")# module_name = "MyWebFrameWork"# app_name = "app"m = __import__(module_name)app = getattr(m, app_name)http_server = HTTPServer(app)# http_server.set_porthttp_server.bind(9000)http_server.start()if __name__ == '__main__':main()

 

 

框架的实现

MyWebFrameWork_v2.py

# coding:utf-8import time# 设置静态文件根目录
HTML_ROOT_DIR = "./html"class Application(object):"""框架的核心部分,也就是框架的主题程序,框架是通用的"""def __init__(self, urls):# 设置路由信息self.urls = urlsdef __call__(self, env, start_response):path = env.get("PATH_INFO", "/")# /static/index.htmlif path.startswith("/static"):# 要访问静态文件file_name = path[7:]# 打开文件,读取内容try:file = open(HTML_ROOT_DIR + file_name, "rb")except IOError:# 代表未找到路由信息,404错误status = "404 Not Found"headers = []start_response(status, headers)return "not found"else:file_data = file.read()file.close()status = "200 OK"headers = []start_response(status, headers)return file_data.decode("utf-8")for url, handler in self.urls:#("/ctime", show_ctime)if path == url:return handler(env, start_response)# 代表未找到路由信息,404错误status = "404 Not Found"headers = []start_response(status, headers)return "not found"def show_ctime(env, start_response):status = "200 OK"headers = [("Content-Type", "text/plain")]start_response(status, headers)return time.ctime()def say_hello(env, start_response):status = "200 OK"headers = [("Content-Type", "text/plain")]start_response(status, headers)return "hello frawework"def say_haha(env, start_response):status = "200 OK"headers = [("Content-Type", "text/plain")]start_response(status, headers)return "hello haha"urls = [("/", show_ctime),("/ctime", show_ctime),("/sayhello", say_hello),("/sayhaha", say_haha),]
app = Application(urls)

 

运行上述代码需要pycharm中配置

服务器程序--Edit Configurations....

 

转载于:https://www.cnblogs.com/wgDream/p/7536904.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/399787.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android中shape的使用

本人在美工方面一直是比较白痴的&#xff0c;对于一些颜色什么乱七八糟的非常头痛&#xff0c;但是在Android编程中这又是经常涉及到的东西&#xff0c;没办法&#xff0c;只有硬着头皮上。 Android中常常使用shape来定义控件的一些显示属性&#xff0c;今天看了一些shape的使用…

PHP遍历数组的几种方法

这三种方法中效率最高的是使用foreach语句遍历数组。从PHP4开始就引入了foreach结构&#xff0c;是PHP中专门为遍历数组而设计的语句&#xff0c;推荐大家使用。先分别介绍这几种方法 PHP中遍历数组有三种常用的方法&#xff1a; 一、使用for语句循环遍历数组&#xff1b; 二、…

Jmeter集合ant进行操作

1、下载ant包 地址【http://ant.apache.org/bindownload.cgi】 2、解压后&#xff0c;配置ant的环境变量&#xff0c;如下图 3、修改jmeter/extras中的build.xml的文件 代码如下&#xff1a; <?xml version"1.0" encoding"UTF-8"?><project nam…

五种常见的 PHP 设计模式

设计模式只是为 Java™ 架构师准备的 —— 至少您可能一直这样认为。实际上&#xff0c;设计模式对于每个人都非常有用。如果这些工具不是 “架构太空人” 的专利&#xff0c;那么它们又是什么&#xff1f;为什么说它们在 PHP 应用程序中非常有用&#xff1f;本文解释了这些问题…

jquery 点击事件

$("#id").click(function() {alert(alert);});

linux常见命令的常用方法示例

本文涉及命令&#xff1a;date,clock,hwclock,cal,ls,cd,pwd,tty,whereis,which,stat,echo,shutdown,halt,reboot,poweroff,who,w,whami部分命令结果等同&#xff0c;合到一起示例一、Date 打印或设置系统日期和时间1、date &#xff1a;查看当前系统日期和时间2、date %a:查看…

Leetcode: LRU Cache

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(k…

Day-17: 网络编程

---恢复内容开始--- 现有的互联网通讯方式&#xff0c;是服务器端的进程与客户端进程的通信。Python中进行网络编程&#xff0c;就是在Python程序本身这个进程内&#xff0c;连接别的服务器进程的通信端口进行通信。 互联网协议上包含了上百种协议标准&#xff0c;但是&#xf…

启动MySQL报错

当在linux系统中启动mysql服务的时候service mysqld start时 报&#xff1a;Another MySQL daemon already running with the same unix socket. 解决办法。 原因多个Mysql进程使用了同一个socket。 两个方法解决&#xff1a; 第一个是立即关机 使用命令 shutdown -h now 关机&…

计算机应用基础教程作业脑图 车辆工程学院 冯大昕

转载于:https://www.cnblogs.com/FengTang/p/7553055.html

UML该元素的行为为基础的元素

&#xfeff;&#xfeff;Behavioral thingsare the dynamic parts of UML models. These are the verbs of a model, representing behavior over time and space. In all, there are three primary kinds of behavioral things. 行为元件是UML模型的动态部分&#xff0e;它们…

EasyDSS高性能流媒体服务器前端重构(五)- webpack + vue-router 开发单页面前端实现按需加载 - 副本...

为了让页面更快完成加载, 第一时间呈现给客户端, 也为了帮助客户端节省流量资源, 我们可以开启 vue-router 提供的按需加载功能, 让客户端打开页面时, 只自动加载必要的资源文件, 当客户端操作页面, 切换功能模块, 触发页面路由变化时, 再去加载相应需要的资源. 本系列博客的前…

可工作的软件胜过面面俱到的文档

正在看一本书&#xff0c;书上讲到一个观点&#xff0c;“可工作的软件胜过面面俱到的文档”&#xff0c;这一点我一直都很认可&#xff0c;在平时工作中也是依据这一观点不写详细设计文档就开始编码&#xff0c;设计过程只做比较粗略的概要设计&#xff0c;但是涉及到数据库设…

解密阿里云七武器之高性能消息服务ONS

2019独角兽企业重金招聘Python工程师标准>>> 7月22日&#xff0c;首届阿里云分享日上&#xff0c;阿里云正式对外发布了企业级互联网架构解决方案&#xff0c;该服务由EDAS应用框架、ONS消息队列、DRDS分布式数据库组成&#xff0c;能有效解决企业上云后网站过载、性…

php动态数组的用法

// 创建连接 $con new mysqli($servername, $username, $password, $dbname); // Check conection if ($con->connect_error) {die("连接失败: " . $con->conect_error); }$sql "SELECT * FROM table limit 0,1000"; $result $con->query($sql…

MySQL划重点-查询-条件

查询的基本语法select * from 表名; 消除重复行在 select 后面列前使用distinct 可以消除重复的行 select distinct gender from students;一、条件 使用where子句对表中的数据筛选&#xff0c;结果为true的行会出现在结果集中语法如下 select * from 表名 where 条件;select *…

高数.........

高数70你敢信&#xff1f;&#xff1f;&#xff1f;临考前各种复习做卷子 各种认真 考试的时候感觉题目也都能做 尼玛连上平时分才给我70&#xff1f; 特么我是不信 打电话问问老师 如果没录错果断重修 郁闷转载于:https://www.cnblogs.com/ahu-shu/p/3520249.html

windows服务器下的ftp server搭建

软件下载链接&#xff1a;http://pan.baidu.com/s/1eQJbmUY ftpserver1.下载后打开。2.运行安装3.安装目录选择。这里我选择安装在C盘的FTP目录下&#xff0c;直接填写即可。这个安装目录可随意设置。4.安装启动。查看使用教程&#xff0c;添加用户名&#xff0c;设置密码&a…

WordPress获取当前分类ID的四种方法

WordPress获取当前分类ID的四种方法 时间: 2015-01-05 所属栏目: Wordpress教程 作者: WP管理员之家 关键词: wordpress,分类ID 关注热度&#xff1a; 4,346 次 (1条) WordPress获取当前分类ID的方法有很多&#xff0c;今天我来给大家总结一下吧,wordpress主题定制专家-WP管理…

linux笔记_20150825_linux下的软件工具唠叨下

这些都是书上看到的&#xff0c;有些工具我也没有完全用过。先记下来再说。闲着也是闲着。 1.linux下常见的语言及编程环境:c/c/java/perl/fortan等. 2.图形环境:gnome/kde/gimp/windowmaker/icewm等. 3.编辑器:xemacs/vim/gedit/pico等. 4.shells&#xff1a;bash/tcsh/ash/cs…