深入了解一下PYTHON中关于SOCKETSERVER的模块-B
2023-09-14 08:59:37 时间
这个是最草的情况,就是硬编码到内存中的字符串,
真实的应用还是会转到其它端口处理,或是读到硬盘上的文件吧。
![复制代码](http://common.cnblogs.com/images/copycode.gif)
#!/usr/bin/env python from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler import time starttime = time.time() class RequestHandler(BaseHTTPRequestHandler): """Definintion of the request hadnler.""" def _writeheaders(self, doc): """Write the HTTP headers for the document. If there is no documnet, send a 404 error code;""" if doc is None: self.send_response(404) else: self.send_response(200) #Always server up HTML for now. self.send_header(Content-type, text/html) self.end_headers() def _getdoc(self, filename): """Handle a request for a document """ global starttime if filename == /: return """ HTML HEAD TITLE sample python page /TITLE /HEAD BODY THIS IS A SAMPLE PAGE. /BODY /HTML elif filename == /stats.html: return """ HTML HEAD TITLE Status /TITLE /HEAD BODY This server has veen running for %d seconds. /BODY /HTML """ % int(time.time() - starttime) else: return None def do_HEAD(self): """Handle a request for headers only""" doc = self._getdoc(self.path) self._writeheaders(doc) def do_GET(self): """Handle a request for headers and body""" doc = self._getdoc(self.path) self._writeheaders(doc) if doc is None: self.wfile.write(""" HTML HEAD TITLE Not found /TITLE /HEAD BODY The requested document %s was not found. /BODY /HTML """ % self.path) else: self.wfile.write(doc) serveraddr = (,8765) srvr = HTTPServer(serveraddr, RequestHandler) srvr.serve_forever()
![复制代码](http://common.cnblogs.com/images/copycode.gif)
python3操作socketserver socketserver是标准库中的一个高级模块,用于网络客户端与服务器的实现.模块中,已经实现了一些可供使用的类. 在python3中,导入socketserver模块使用的命令: import socketserver socketserver模块包括许多可以简化TCP,UDP,UNIX域套接字服务器实现的类.
相关文章
- Python基础23-SSH模块paramiko
- tensorflowpycharm安装_python tensorflow安装
- pyinstaller打包python-docx报错 No such file or directory (default-header.xml)
- Python中通过PyPDF2实现PDF拆分「建议收藏」
- Python元组_python输出元组
- Python入门与基础刷题篇(8)
- 【说站】python怎么导入pygame模块
- Python - 警告模块 warnings
- python的datetime模块的实用功能
- python接收邮件
- 用Python发送通知到企业微信,实现消息推送
- Python基础语法-内置函数和模块-loging模块
- Python面向对象编程-魔术方法-__str__和__repr__方法
- python对MySqldb模块的简单封装代码详解编程语言
- 小白的Python之路 day5 python模块详解及import本质编程语言
- 在Python中简单调用MySQL(python调用mysql)
- Linux系统下安装Python模块指南(linux安装python模块)
- Python语言编写电脑时间自动同步小工具
- python基础教程之数字处理(math)模块详解
- Python入门篇之数字
- python字典多条件排序方法实例