我們都知道,沒有付費的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按鈕,在手機的瀏覽器上粘貼后訪問。