计算机网络课程实验1——WINDOWS 环境下实现 WEB 服务器(python)
2023-09-11 14:22:52 时间
代码部分如下:
# coding=utf-8
from http.server import BaseHTTPRequestHandler, HTTPServer
PORT_NUMBER = 8080
# This class will handles any incoming request from
# the browser
class myHandler(BaseHTTPRequestHandler):
# Handler for the GET requests
def do_GET(self):
if self.path == "/":
self.path = "./html/from.html"
if self.path == "/index.html":
self.path = "./html/to.html"
try:
# Check the file extension required and
# set the right mime type
sendReply = False
if self.path.endswith(".html"):
mimetype = 'text/html' # mimetype用来指示文件类型的字符串
sendReply = True
if self.path.endswith(".jpg"):
mimetype = 'image/jpg'
sendReply = True
if self.path.endswith(".gif"):
mimetype = 'image/gif'
sendReply = True
if self.path.endswith(".js"):
mimetype = 'application/javascript'
sendReply = True
if self.path.endswith(".css"):
mimetype = 'text/css'
sendReply = True
if sendReply:
# Open the static file requested and send it
f = open(self.path)
self.send_response(200)
self.send_header('Content-type', mimetype)
self.end_headers()
self.wfile.write(f.read().encode('utf-8'))
f.close()
return
except IOError:
self.send_error(404, 'File Not Found: %s' % self.path)
try:
# Create a web server and define the handler to manage the
# incoming request
server = HTTPServer(('127.0.0.1', PORT_NUMBER), myHandler)
print('Started httpserver on port ', PORT_NUMBER)
# Wait forever for incoming http requests
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down the web server')
server.socket.close()
代码分析:
本次实验通过Python程序实现web服务器的功能,其中代码的前半段是对于程序文件目录中的HTML文件进行相对路径到绝对路径的调整和修正,确保程序可以正确的找到我们指定的和HTML文件以及图片等网页资源。
代码的后半段,则是整个程序实现web功能的主要部分,主要为以下几个函数,共同实现get请求的处理。
self.send_response(200)
self.send_header('Content-type', mimetype)
self.end_headers()
self.wfile.write(f.read().encode('utf-8'))
代码实现效果:运行Python程序后进入localhost8080出现一下界面:
点击next_page之后,出现另一个界面:
实验心得与体会
通过这次实验,我对html协议有了更深刻的了解。知道了怎么用Python搭建一个web服务器。并进行简单的网页跳转和8080页面编写。对于HTTP 协议下客户/服务器模式中信息交换的实现原理、流程(建立连接、发送请求、发送响应、关闭连接),相比于理论知识,在动手实践后能更好地掌握和理解。
相关文章
- Python yaml模块使用
- python里面list()函数
- Windows 由于无法验证发布者,windows阻止控件安装怎么办
- [Python模块学习]用qrcode模块生成二维码
- [LINK]Python服务器开发一:python基础
- 解决安装Python版本出现No module named ‘apt_pkg‘问题
- Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略
- Python编程语言学习:包导入和模块搜索路径(包路径)简介、使用方法(python系统环境路径的查询与添加)之详细攻略
- 成功解决:将后缀.pyx格式文件(linux环境)编译成pyd文件(windows环境下)实现python编程加载或导入
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Find/undo事件)
- Python语言学习之双下划线那些事:python和双下划线使用方法之详细攻略
- Python每日一练(机器学习篇)——第44天:机器学习
- 〖Python零基础入门篇⑱〗- Python字符串常用方法
- Python 修改pip源---windows / Linux
- API接口开发其实特简单,Python FastApi Web 框架教程来了
- Python 23种设计模式全(python例子)
- Python: yield, python 实现tail -f
- Windows下安装MySQLdb, Python操作MySQL数据库的增删改查
- Python的包管理工具Pip
- python里把dir函数当作查看成员输出
- python基础===利用PyCharm进行Python远程调试(转)
- 华为校招机试 - 简单的自动曝光、平均像素值(Java & JS & Python)
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- Windows安装WSL进行机器学习(windows和Ubuntu同时使用)
- windows下运行python文件路径总是出错?
- Python ---- windows 10 虚拟环境搭建
- Python .py 文件打包成 .exe 文件(Windows平台,python 3.x)