socket 模拟 HTTP 服务
基于 socket 开发,监听 127.0.0.1 任一端口,如:8888,接收监听到的数据,并通过 conn 以 HTTP 响应的格式返回数据
import socket
sock = socket.socket()
sock.bind(("localhost", 8888)) # 绑定监听的 IP 地址与端口 8800
sock.listen(5) # 设置最大监听数
while True:
conn, addr = sock.accept()
data = conn.recv(1024)
print(data) # 打印查看请求头与请求体
# 必须以 HTTP 响应头的格式返回数据,否则浏览器无法正常解析
# 同时注意 send 的数据不能是 str 字符串,必须是 bytes,否则会报错。
conn.send(b'HTTP/1.1 200 OK\r\n\r\n <h1>Hello world!</h1>')
conn.close()
浏览器访问 localhost:8800,即可看到网页结果,http response 中响应体前必须有两空行’ \r\n\r\n’ 否则会被认为是响应头的内容
https://blog.csdn.net/qq_29941979/article/details/107871763
Tornado 库 class="reference-link" href="#">利用 Tornado 库
my.py
import tornado.ioloop
import tornado.web
#访问地址 http://127.0.0.1:9870/main?ywdm=06&num1=10&num2=200
class TestClassA:
def sub(self,a,b):
return a-b
def add(self,a,b):
return a+b
def chen(self,a,b):
return a*b
class TestClassB:
def sub(self,a,b):
return a-b
def add(self,a,b):
return a+b
def chen(self,a,b):
return a*b
sys_config={}
sys_config["01"]=['mymvc','TestClassA','add']
sys_config["02"]=['mymvc','TestClassA','sub']
sys_config["03"]=['mymvc','TestClassA','chen']
sys_config["04"]=['mymvc','TestClassB','add']
sys_config["05"]=['mymvc','TestClassB','sub']
sys_config["06"]=['mymvc','TestClassB','chen']
class MainHandler(tornado.web.RequestHandler):
def get(self):
ywdm=self.get_argument('ywdm')
num1=int(self.get_argument('num1').encode('utf-8'))
num2=int(self.get_argument('num2').encode('utf-8'))
message="hello !"
print type(num2)
if ywdm in sys_config:
my_module_name=sys_config[ywdm][0]
my_class_name=sys_config[ywdm][1]
my_method_name=sys_config[ywdm][2]
my_module = __import__(my_module_name)
my_class = getattr(my_module,my_class_name)
my_obj = my_class()
my_method = getattr(my_obj,my_method_name)
ret=my_method(num1,num2)
print "ret:",ret
#message = "ret data:::"+ret
message="ywdm:"+ywdm+"-data:"+str(ret)
items = ["Item 1", "Item 2", "Item 3"]
self.render("test.html", title="My title", items=items,config_items=sys_config)
#self.write(message)
#self.finish()
application = tornado.web.Application([(r"/main", MainHandler),])
if __name__ == "__main__":
application.listen(9870)
tornado.ioloop.IOLoop.instance().start()
test.html
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<ul>
{% for item in items %}
<li>{{escape(item) }}</li>
{% end %}
</ul>
<ul>
{% for item in config_items %}
<li>{{escape(config_items[item][0]) }}
-{{escape(config_items[item][1]) }}
-{{escape(config_items[item][2]) }}
</li>
{% end %}
</ul>
</body>
</html>
https://blog.csdn.net/5iasp/article/details/23267609
其中 py 中 application 中为数组形式,可以加多条映射,返回结果可以返回渲染后模板也可以返回字符串,例如:
import tornado.ioloop
import tornado.web
import ctypes
class MainHandler(tornado.web.RequestHandler):
def get(self):
ip = self.get_argument('ip')
print(ip)
self.write(" 成功 ")
self.finish()
class IndexHandler(tornado.web.RequestHandler):
def get(self):
items = []
self.render("ip.html", title="My title", items=items)
application = tornado.web.Application([(r"/index/get", MainHandler),(r"/index", IndexHandler) ])
if __name__ == "__main__":
# 最小化程序
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 6)
application.listen(9870)
tornado.ioloop.IOLoop.instance().start()
目录
socket 模拟 HTTP 服务
利用 Tornado 库
目录
socket 模拟 HTTP 服务
利用 Tornado 库