我们都知道,没有付费的cpolar用户,要不时更新cpolar网址。每次都要自己登录去获取远程的cpolar个人网址比较麻烦,好在可以用python去自动获取。这里说的是,比如用手机装termux软件,再在termux中装cpolar,然后我用qpython软件制作一个网站,网站的功能是利用qpython的sl4a拍照功能不断拍摄,保存到数据库,以网页显示实现远程监控功能,用qpython中flask开启一个127.0.0.1端口为默认5000的网站,用cpolar穿透到互联网上。然后就回到我一开始说的获取cpolar生成的随机网址。考虑到使用便捷,制作一个安卓程序,装在手机上随时能查看监控的情况。
上面,废话很多,还是具体用到的人就会明白。
一,装briefcase,即beeware。用briefcase new生成一个空间,修改app.py为
"""
qwcy app
"""import toga
from toga.style.pack import COLUMN, LEFT, RIGHT, ROW, Pack
import requests
from bs4 import BeautifulSoup#导入必要的库def fetch_info_from_website(login_url, info_url, credentials, tunnel_name):with requests.Session() as session:try:# 获取登录页面以抓取csrf tokenlogin_page = session.get(login_url)login_page.raise_for_status() # 检查请求是否成功login_page_soup = BeautifulSoup(login_page.text, 'html.parser')# 提取csrf tokencsrf_token = login_page_soup.find('input', {'name': 'csrf_token'})['value']credentials['csrf_token'] = csrf_token# 登录print("登录中,请等待。")login_response = session.post(login_url, data=credentials)# 检查是否登录成功if login_response.status_code != 200 or login_response.url == login_url:print("登录失败,请检查您的凭据。")return []else:print("登录成功。")# 获取信息页面response = session.get(info_url)response.raise_for_status()# 解析页面soup = BeautifulSoup(response.text, 'html.parser')table = soup.find('table')if not table:print("未找到隧道列表,请检查对应设备的cpolar服务和网络连接。")return []links = [] # 用于存储找到的链接for row in table.find_all('tr')[1:]: # 跳过表头cells = row.find_all('td')if len(cells) > 1:tunnel = cells[0].get_text().strip()url_cell = row.find('a', href=True) # 直接在行中查找<a>标签if tunnel == tunnel_name and url_cell:links.append(url_cell['href']) # 添加匹配的链接print(f"找到隧道 {tunnel} 的链接: {url_cell['href']}")return linksexcept requests.RequestException as e:print(f"请求异常: {e}")except Exception as e:print(f"发生错误: {e}")
def build(app):# 定义组件box = toga.Box()webview = toga.WebView(style=Pack(width=600, height=800,padding_left=2))login_url = "https://dashboard.cpolar.com/login"info_url = "https://dashboard.cpolar.com/status"credentials = {'login': '你的号@qq.com','password': '你的密码'}tunnel_name = 'default'links = fetch_info_from_website(login_url, info_url, credentials, tunnel_name)if links:geturl = links[1]else:geturl = '无法获取网址,重新开启'html = f'''<script> function copyTextToClipboard(text) {{var input = document.createElement('input');document.body.appendChild(input);input.setAttribute('value', text);input.select();var result = document.execCommand('copy');document.body.removeChild(input);return result;}}</script> <a href='{geturl}'>{geturl}</a><br>{links[0]}<br><button onclick="copyTextToClipboard('{links[0]}')">Copy Url</button>'''webview.set_content("data:text/html", html)# webview.url = geturlbox.add(webview)return boxdef main():return toga.App("查看小孩学习网址", "org.geturl", startup=build)if __name__ == "__main__":main().main_loop()
二,修改pyproject.toml文件。我的为
# This project was generated with 0.3.17 using template: https://github.com/beeware/briefcase-template@v0.3.17
[tool.briefcase]
project_name = "geturl"
bundle = "com.geturl"
version = "0.0.1"
url = "https://geturl.com/geturl"
license = "MIT license"
author = "ybk"
author_email = "eybk@qq.com"[tool.briefcase.app.geturl]
formal_name = "geturl"
description = "geturlapp"
long_description = """More details about the app should go here.
"""
icon = "src/geturl/resources/geturl"
sources = ["src/geturl",
]
test_sources = ["tests",
]requires = ["requests","beautifulsoup4"
]
test_requires = [ "pytest",][tool.briefcase.app.geturl.macOS]
universal_build = true
requires = ["toga-cocoa~=0.4.0","std-nslog~=1.0.0",
][tool.briefcase.app.geturl.linux]
requires = ["toga-gtk~=0.4.0",
][tool.briefcase.app.geturl.linux.system.debian]
system_requires = [# Needed to compile pycairo wheel"libcairo2-dev",# Needed to compile PyGObject wheel"libgirepository1.0-dev",
]system_runtime_requires = [# Needed to provide GTK and its GI bindings"gir1.2-gtk-3.0","libgirepository-1.0-1",# Dependencies that GTK looks for at runtime"libcanberra-gtk3-module",# Needed to provide WebKit2 at runtime# "gir1.2-webkit2-4.0",
][tool.briefcase.app.geturl.linux.system.rhel]
system_requires = [# Needed to compile pycairo wheel"cairo-gobject-devel",# Needed to compile PyGObject wheel"gobject-introspection-devel",
]system_runtime_requires = [# Needed to support Python bindings to GTK"gobject-introspection",# Needed to provide GTK"gtk3",# Dependencies that GTK looks for at runtime"libcanberra-gtk3",# Needed to provide WebKit2 at runtime# "webkit2gtk3",
][tool.briefcase.app.geturl.linux.system.suse]
system_requires = [# Needed to compile pycairo wheel"cairo-devel",# Needed to compile PyGObject wheel"gobject-introspection-devel",
]system_runtime_requires = [# Needed to provide GTK"gtk3",# Needed to support Python bindings to GTK"gobject-introspection", "typelib(Gtk) = 3.0",# Dependencies that GTK looks for at runtime"libcanberra-gtk3-0",# Needed to provide WebKit2 at runtime# "libwebkit2gtk3",# "typelib(WebKit2)",
][tool.briefcase.app.geturl.linux.system.arch]
system_requires = [# Needed to compile pycairo wheel"cairo",# Needed to compile PyGObject wheel"gobject-introspection",# Runtime dependencies that need to exist so that the# Arch package passes final validation.# Needed to provide GTK"gtk3",# Dependencies that GTK looks for at runtime"libcanberra",# Needed to provide WebKit2# "webkit2gtk",
]system_runtime_requires = [# Needed to provide GTK"gtk3",# Needed to provide PyGObject bindings"gobject-introspection-runtime",# Dependencies that GTK looks for at runtime"libcanberra",# Needed to provide WebKit2 at runtime# "webkit2gtk",
][tool.briefcase.app.geturl.linux.appimage]
manylinux = "manylinux_2_28"system_requires = [# Needed to compile pycairo wheel"cairo-gobject-devel",# Needed to compile PyGObject wheel"gobject-introspection-devel",# Needed to provide GTK"gtk3-devel",# Dependencies that GTK looks for at runtime, that need to be# in the build environment to be picked up by linuxdeploy"libcanberra-gtk3","PackageKit-gtk3-module","gvfs-client",
]linuxdeploy_plugins = ["DEPLOY_GTK_VERSION=3 gtk",
][tool.briefcase.app.geturl.linux.flatpak]
flatpak_runtime = "org.gnome.Platform"
flatpak_runtime_version = "45"
flatpak_sdk = "org.gnome.Sdk"[tool.briefcase.app.geturl.windows]
requires = ["toga-winforms~=0.4.0",
]# Mobile deployments
[tool.briefcase.app.geturl.iOS]
requires = ["toga-iOS~=0.4.0","std-nslog~=1.0.0",
][tool.briefcase.app.geturl.android]
requires = ["toga-android~=0.4.0",
]base_theme = "Theme.MaterialComponents.Light.DarkActionBar"build_gradle_dependencies = ["androidx.appcompat:appcompat:1.6.1","com.google.android.material:material:1.11.0",# Needed for DetailedList"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
]# Web deployments
[tool.briefcase.app.geturl.web]
requires = ["toga-web~=0.4.0",
]
style_framework = "Shoelace v2.3"
即添加requires = [
"requests",
"beautifulsoup4"
]
三,briefcase create android,briefcase build android生成app-debug.apk,把这个文件发给手机,就安装可以打开。
界面如上,用https才能在app中点进去访问,但不能调网页大小,还是用JavaScript,点击copy url按钮,在手机的浏览器上粘贴后访问。