python3实现微型的web服务器
作者:五力 时间:2021-03-03 09:00:49
实验目的:用socket 模拟一个微型的web服务器,当py脚本run起后,实微型web server架起了,然后用本地浏览器访问127.0.0.1:8080(web server的ip_port)时web服务器就会将网页内容传给浏览器,实现网页浏览.
sw+sys: python3.7.2 + windows10 64bit
本地准备的server端网页为下载的hao123主页(我已上载并上传,点击这里)
通过这个实验让我学到了:
1. 当get请求一个主页时,要完整的显示一个页面(包括文本、图片、css绚染等)是要get多次请求的。
2. respone回复本地页网,open(filepath, rwa)时要特别的注意
import socket
import os
curfilepath = os.path.split(os.path.realpath(__file__))[0].replace("\\" , "/")
print(f'curfilepath: {curfilepath}')
def new_socket_server(new_socket, new_addr):
if new_addr[0] != '':
print(f'当前客户端{new_addr}已连接上server端. ')
# 3.接收信息
file_name = ''
request_data = new_socket.recv(1024).decode('utf-8')
if request_data != '':
print(f'有收到新的信息,信息如下:\n{request_data}')
file_name = request_data.splitlines()[0].split(' ')[1]
print(f'file_name: {file_name}')
if file_name == '/':
file_name = '/index.html'
print(f'file_name: {file_name}')
with open(curfilepath + '/test.txt', 'a+') as f:
f.write(file_name + '\n')
# 4.回复信息
try:
f = open(curfilepath + '/htmltest' + file_name, 'rb')
except:
response = 'HTTP/1.1 404 NOT FOUND\r\n'
response += '\r\n'
response += '----------file not found-------'
new_socket.send(response.encode('utf-8'))
else:
html_content = f.read()
f.close()
response = 'HTTP/1.1 200 OK\r\n' + '\r\n'
new_socket.send(response.encode('utf-8'))
new_socket.send(html_content)
def main():
# 1.创建socket
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2.连接server
server_ip_port = ('127.0.0.1', 8080)
tcp_server_socket.bind(server_ip_port)
tcp_server_socket.listen(128)
while True:
print('正在等待client端连接... ...')
new_socket, new_addr = tcp_server_socket.accept()
new_socket_server(new_socket, new_addr)
new_socket.close()
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/lxy210781/article/details/88372456
标签:python3,web服务器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于“简单,可依赖”
2008-10-22 13:33:00
匿名函数的写法
2010-01-06 13:32:00
SQL Server 2005代理服务应用软件组成
2009-02-19 17:19:00
SQL Server 安装出错:以前的某个程序安装已在安装计算机上创建挂起的文件操作
2010-02-23 14:48:00
laravel 实现阿里云oss文件上传功能的示例
2023-06-13 20:39:26
![](https://img.aspxhome.com/file/2023/2/55532_0s.jpg)
php中让上传的文件大小在上传前就受限制的两种解决方法
2023-10-25 17:53:12
asp获取文件md5值
2008-10-13 09:10:00
Mysql中日期和时间函数介绍
2008-05-24 08:16:00
Golang数据类型比较详解
2023-07-17 10:11:21
![](https://img.aspxhome.com/file/2023/5/93665_0s.jpg)
2003年至今网页尺寸增长3倍
2008-04-29 19:27:00
![](https://img.aspxhome.com/file/UploadPic/20084/29/growth-average-web-page-sm_23s.gif)
asp如何读取Windows的信息文件(.ini)?
2009-11-20 18:27:00
如何使用数据绑定控件实现不换页提交数据?
2010-05-16 15:17:00
Go中的应用配置管理详解
2023-06-21 00:40:55
讲解Access数据库中数据表的自动重新联接
2008-11-28 16:29:00
在JScript中使用ADODB.Stream判断文件编码
2008-06-08 13:03:00
请正确认识MySQL对服务器端光标的限制
2008-12-17 14:58:00
让IE8支持eWebEditor在线编辑器
2010-02-28 10:36:00
[设计]DREAMWEAVER 问题集锦
2010-09-02 12:31:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113121103s.jpg)
JavaScript十二月新标准ECMA262v5快速浏览
2009-12-27 12:56:00
python使用jieba实现中文分词去停用词方法示例
2021-02-04 11:27:17