python静态web服务器如何实现
python静态web服务器如何实现的方法:
1、编写TCP服务器程序。
2、获取浏览器发送的http请求消息数据。
3、读取固定的页面数据,将页面数据组装成HTTP响应消息数据并发送给浏览器。
4、HTTP响应报文数据发送完成后,关闭服务于客户端的套接字。
实例
#时间:2021/10/2120:38
importsocket
if__name__=='__main__':
#创建tcp服务端套接字
tcp_server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#设置端口号复用,程序退出端口立即释放
tcp_server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)
#绑定端口号
tcp_server_socket.bind(("",8080))
#设置监听
tcp_server_socket.listen(128)
whileTrue:
#等待接受客户端的连接请求
new_socket,ip_port=tcp_server_socket.accept()
#代码执行到此,说明连接建立成功
recv_client_data=new_socket.recv(4096)
#对二进制数据进行解码
recv_client_content=recv_client_data.decode)
print(recv_client_content)
#响应行
response_line="HTTP/1.1200OK\r\n"
#响应头
response_header="Server:py1.0\r\n"
#响应体
response_body="Hello,guys!"
#拼接响应报文
response_data=(response_line+response_header+"\r\n"+response_body).encode()
#发送数据
new_socket.send(response_data)
#关闭服务与客户端的套接字
new_socket.close()
以上内容为大家介绍了python培训之静态web服务器如何实现,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python字符串需要注意的语法问题
python字符串需要注意的语法问题语法错误是经常发生的一个错误。当一个程序包含非法的Python代码时,会产生语法错误。举例来说,在包含单引号的...详情>>
2023-11-14 18:23:24
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15热门推荐
python中title函数的作用
沸python中集合与列表有什么区别?
热python中uuid模块是什么?
热python中使用_setattr_()
新python中scikit-learn是什么?
python Faker库如何使用?
python behold库是什么?
python中ndarray是什么?
python UDP编程是什么意思?
python如何创建UDP服务器和客户端?
python字符串需要注意的语法问题
python如何配置Logging处理器
pythonos模块有几种用法?
python如何定制日志输出格式
技术干货






