当前位置: 首页 > news >正文

站群 网站如何做万网域名注册查询

站群 网站如何做,万网域名注册查询,北京南站,2021要打仗了是真的吗在接触的一些项目中,有时为了方便可视化一些服务状态(请求数很少),那么很容易想到使用http服务来实现。但开源的web后端框架,例如flask,fastapi,django等略显沉重,且使用这些框架会有…

在接触的一些项目中,有时为了方便可视化一些服务状态(请求数很少),那么很容易想到使用http服务来实现。但开源的web后端框架,例如flask,fastapi,django等略显沉重,且使用这些框架会有各种各样的限制,为了更加灵活的使用,可以自己通过Python自带的socket库来实现,下面是我简单实现的代码,贴在这里方便后续使用。

客户端代码:

import os
import time
import json
import socket
import datetime
import traceback
from urllib import parse
from concurrent.futures import ThreadPoolExecutorENCODING = "utf-8"class CustomRouter:def __init__(self):self.router_dict = {}def register(self, url: str, method: str):if method not in ["GET", "POST"]:raise ValueError(f"method only support GET or POST, got {method}")key = f"{url}:{method}"if key in self.router_dict:raise ValueError(f"url:{url} method:{method} already registed")def _register(func):self.router_dict[key] = funcreturn funcreturn _registerdef has_register(self, url: str, method: str):key = f"{url}:{method}"return key in self.router_dictdef request(self, url: str, method: str):key = f"{url}:{method}"func = self.router_dict[key]return func()router = CustomRouter()@router.register(url="/", method="GET")
def task():return "Hello World!!!"@router.register(url="/get_project_info", method="GET")
def get_project_info():info = {"pwd": os.path.abspath(os.curdir),"encoding": ENCODING,"time": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}return json.dumps(info)class CustomServer:def __init__(self, port: int = 5055, backlog: int = 2, num_workers: int = 2):self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)self.server.bind(("", port))self.server.listen(backlog)self.buff_size = 1024self.encoding = ENCODINGself.router = routerself.timeout = 60self.thread_pool = ThreadPoolExecutor(max_workers=num_workers)def parse_message(self, message: str):method, url_params, message = message.split(" ", maxsplit=2)res = parse.urlparse(url_params)url = res.pathparams = dict([i for i in parse.parse_qsl(res.query)])version, message = message.split("\r\n", maxsplit=1)return method, url, params, versiondef task(self, conn: socket.socket, addr):conn.settimeout(self.timeout)try:message = conn.recv(self.buff_size).decode(self.encoding)t0 = time.perf_counter()method, url, params, version = self.parse_message(message)if not self.router.has_register(url, method):conn.send("HTTP/1.1 501 Not Implemented\r\n\r\n".encode(self.encoding))print(f"[{method}][501]client addr: {addr} request: {url}, time: {time.perf_counter() - t0:.5f}")conn.close()returnres = self.router.request(url, method)conn.send("HTTP/1.1 200 OK\r\n\r\n".encode(self.encoding))conn.send(res.encode(self.encoding))print(f"[{method}][200]client addr: {addr} request: {url}, time: {time.perf_counter() - t0:.5f}")except (BrokenPipeError, TimeoutError):print(traceback.format_exc())except:print(traceback.format_exc())conn.send("HTTP/1.1 500 Inner Error\r\n\r\n".encode(self.encoding))print(f"[{method}][500]client addr: {addr} request: {url}, time: {time.perf_counter() - t0:.5f}")finally:conn.close()def run(self):while True:conn, addr = self.server.accept()self.thread_pool.submit(self.task, conn, addr)if __name__ == '__main__':s = CustomServer()s.run()

客户端代码:

import requestsdef main():for _ in range(50):res = requests.get("http://127.0.0.1:5055")print(res.status_code, res.text)res = requests.get("http://127.0.0.1:5055/get_project_info")print(res.status_code, res.json())if __name__ == '__main__':main()

客户端请求时,服务端终端输出:

[GET][200]client addr: ('127.0.0.1', 38054) request: /, time: 0.00002
[GET][200]client addr: ('127.0.0.1', 38058) request: /get_project_info, time: 0.00004
[GET][200]client addr: ('127.0.0.1', 38060) request: /, time: 0.00002
[GET][200]client addr: ('127.0.0.1', 38070) request: /get_project_info, time: 0.00004
[GET][200]client addr: ('127.0.0.1', 38080) request: /, time: 0.00002
[GET][200]client addr: ('127.0.0.1', 38096) request: /get_project_info, time: 0.00004
[GET][200]client addr: ('127.0.0.1', 38098) request: /, time: 0.00002
[GET][200]client addr: ('127.0.0.1', 38114) request: /get_project_info, time: 0.00009
[GET][200]client addr: ('127.0.0.1', 38120) request: /, time: 0.00003
[GET][200]client addr: ('127.0.0.1', 38122) request: /get_project_info, time: 0.00006
[GET][200]client addr: ('127.0.0.1', 38136) request: /, time: 0.00003
[GET][200]client addr: ('127.0.0.1', 38146) request: /get_project_info, time: 0.00006
[GET][200]client addr: ('127.0.0.1', 38160) request: /, time: 0.00003
[GET][200]client addr: ('127.0.0.1', 38172) request: /get_project_info, time: 0.00005
[GET][200]client addr: ('127.0.0.1', 38178) request: /, time: 0.00002
[GET][200]client addr: ('127.0.0.1', 38182) request: /get_project_info, time: 0.00005

客户端终端输出:

200 Hello World!!!
200 {'pwd': '/home/wz/my_projects/others/socket_t', 'encoding': 'utf-8', 'time': '2025-01-14 00:20:01'}
200 Hello World!!!
200 {'pwd': '/home/wz/my_projects/others/socket_t', 'encoding': 'utf-8', 'time': '2025-01-14 00:20:01'}
200 Hello World!!!
200 {'pwd': '/home/wz/my_projects/others/socket_t', 'encoding': 'utf-8', 'time': '2025-01-14 00:20:01'}
200 Hello World!!!
200 {'pwd': '/home/wz/my_projects/others/socket_t', 'encoding': 'utf-8', 'time': '2025-01-14 00:20:01'}
200 Hello World!!!
200 {'pwd': '/home/wz/my_projects/others/socket_t', 'encoding': 'utf-8', 'time': '2025-01-14 00:20:01'}
200 Hello World!!!
200 {'pwd': '/home/wz/my_projects/others/socket_t', 'encoding': 'utf-8', 'time': '2025-01-14 00:20:01'}
200 Hello World!!!
200 {'pwd': '/home/wz/my_projects/others/socket_t', 'encoding': 'utf-8', 'time': '2025-01-14 00:20:01'}
200 Hello World!!!
200 {'pwd': '/home/wz/my_projects/others/socket_t', 'encoding': 'utf-8', 'time': '2025-01-14 00:20:01'}
http://www.hkea.cn/news/979479/

相关文章:

  • 专门做宠物食品的网站市场调研怎么做
  • 兰州网站建设q.479185700棒成年s8视频加密线路
  • 付费网站推广seo关键词排名优化怎么收费
  • 网站由那些组成google网页搜索
  • 对一个网站做性能测试谷歌paypal官网入口
  • 北京住房投资建设中心网站首页快速排名怎么做
  • 中国网站制作 第一个佛山网站优化
  • thinkphp做的教育网站微商引流推广
  • 做特卖网站手机版电商最好卖的十大产品
  • 怎样做网站平叿trinseo公司
  • 北京大兴最专业的网站建设公司如何推广一个项目
  • 网页设计最牛的网站建设宁波网站优化公司哪家好
  • 建设通查询如何做网站推广及优化
  • 城乡建设网站首页百度seo收录软件
  • 永久免费建个人网站培训网站建设
  • 如何使用jq做弹幕网站好用的磁力搜索引擎
  • 南充营销型网站建设高端品牌网站建设
  • 制作小程序和网站的公司搜狗收录提交入口网址
  • 手机站电影基础建站如何提升和优化
  • 江苏 网站备案百度贴吧官网app下载
  • 网站制作三站湖南网站seo公司
  • 简单做任务赚钱网站企业管理培训课程报名
  • 零点研究咨询集团官方网站建设相似图片在线查找
  • 网站开发需要什么软件关键词app
  • 360全景网站建设做了5天游戏推广被抓了
  • 政府网站建设经验典型材料河源今日头条新闻最新
  • 为什么要进行网站备案佛山市人民政府门户网站
  • 摄影网站开发背景百度app交易平台
  • 吉林网站建设石家庄百度快照优化排名
  • 大学生网站开发总结报告app推广接单发布平台