
利用clash restapi实现节点切换
- import random
- from utils importHttpUtil
- fromConfigimportConfig
- import json
- classProxyUtil:
- http =HttpUtil()
- def __init__(self):
- pass
- @staticmethod
- def get_proxies():
- res = json.loads(ProxyUtil.http.getString(Config.proxy_url +"/proxies"))
- # print(res)
- proxies = res["proxies"]
- print(proxies)
- return proxies
- pass
- @staticmethod
- def get_nodes():
- res = json.loads(ProxyUtil.http.getString(Config.proxy_url +"/proxies"))
- # print(res)
- proxies = res["proxies"]["Proxy"]["all"]
- print(proxies)
- return proxies
- pass
- @staticmethod
- def get_node(name):
- res = json.loads(ProxyUtil.http.getString(Config.proxy_url +"/proxies/"+ name))
- print(res)
- return res
- @staticmethod
- def switch_node():
- nodes =ProxyUtil.get_nodes()
- node = random.choice(nodes)
- data ={"name": node}
- res =ProxyUtil.http.put(Config.proxy_url +"/proxies/Proxy", data=data)
- print('switch to '+ node)
- return res
- pass
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # auth: bruce
- # date: 2022-06-22 13:42:26
- # Http2.py
- import json
- import random
- import requests
- from lxml import etree
- from Config import Config
- class HttpUtil:
- @staticmethod
- def get(url, params=None, headers=Config.headers):
- Config.headers["User-Agent"] = random.choice(Config.user_agents)
- try:
- page = requests.get(url, params=params, headers=headers, timeout=20, proxies=Config.proxies)
- res = etree.HTML(page.text)
- return res
- except Exception as e:
- print(e)
- return etree.HTML("<html></html>")
- @staticmethod
- def getString(url, params=None):
- Config.headers["User-Agent"] = random.choice(Config.user_agents)
- try:
- page = requests.get(url, params=params, headers=Config.headers, timeout=20, proxies=Config.proxies)
- res = page.text
- return res
- except Exception as e:
- print(e)
- return "<html></html>"
- @staticmethod
- def put(url, data):
- headers = {"Content-type": "application/json", "Accept": "text/plain"}
- payload = json.dumps(data)
- res = requests.put(url, data=payload, headers=headers)
- print(res.status_code)
- return res.status_code
- pass
============ 欢迎各位老板打赏~ ===========


与本文相关的文章
- · shadowsocks/shadowsocks-libev 怎么生成订阅链接
- · Sub-Web搭建教程!自行搭建Clash订阅转换平台,自建Sub-Web前端和SubConverter后端
- · ShellCrash使用的常见问题1
- · clash订阅转换搭建
- · docker布署clash
- · 一行代码安装ssr
- · Docker 快速部署 FastAPI 项目
- · python 四元数 转 欧拉角
- · 曲线(轨迹)相似度算法——LCSS最长公共子序列算法
- · python版DTW动态时间规划算法
- · macOS Charles 4.x版本的安装及使用(含破解激活)
- · centos安装chrome+chromedriver