我如何使用我的 python 脚本作为 url 的代理

How can i use my python script as proxy for urls

我有一个检查输入 link 的脚本,如果它等同于我在代码中指定的一个,那么它将使用我的代码,否则它会在 [=19] 中打开 link =].

我想让那个脚本有点像默认浏览器,与打开浏览器相比速度更快,在扩展的帮助下获得 link 然后使用 POST.

我使用 procmon 检查有问题的进程在哪里查询注册表项,它似乎试图检查 HKCU\Software\Classes\ChromeHTML\shell\open\command 所以我在那里添加了一些密钥,并在命令中编辑了密钥的内容使用我的脚本路径和参数 (-- %1)(-- 此处仅用于测试目的) 不幸的是,一旦程序查询发送 link、windows 提示选择浏览器而不是我的脚本,这不是我想要的。 有什么想法吗?

in HKEY_CURRENT_USER\Software\Classes\ChromeHTML\Shell\open\command 将默认值替换为 "C:\Users\samdra.r\AppData\Local\Programs\Python\Python39\pythonw.exe" "[Script_path_here]" %1

启动 link 时,系统只会要求您设置一次默认浏览器(它会为您对密钥所做的每项更改要求一个默认浏览器): 我 select chrome 在我的例子中

至于python脚本,这里是:

import sys
import browser_cookie3
import requests
from bs4 import BeautifulSoup as BS
import re
import os
import asyncio
import shutil

def Prep_download(args):
    settings = os.path.abspath(__file__.split("NewAltDownload.py")[0]+'/settings.txt')
    if args[1] == "-d" or args[1] == "-disable":
        with open(settings, 'r+') as f:
            f.write(f.read()+"\n"+"False")
            print("Background program disabled, exiting...")
            exit()
    if args[1] == "-e" or args[1] == "-enable":
        with open(settings, 'r+') as f:
            f.write(f.read()+"\n"+"True")
    link = args[-1]
    with open(settings, 'r+') as f:
        try:
            data = f.read()
            osupath = data.split("\n")[0]
            state = data.split("\n")[1]
        except:
            f.write(f.read()+"\n"+"True")
            print("Possible first run, wrote True, exiting...")
            exit()
    if state == "True":
        asyncio.run(Download_map(osupath, link))

async def Download_map(osupath, link):
    if link.split("/")[2] == "osu.ppy.sh" and link.split("/")[3] == "b" or link.split("/")[3] == "beatmapsets":
        with requests.get(link) as r:
            link = r.url.split("#")[0]
        BMID = []
        id = re.sub("[^0-9]", "", link)
        for ids in os.listdir(os.path.abspath(osupath+("/Songs/"))):
            if re.match(r"(^\d*)",ids).group(0).isdigit():
                BMID.append(re.match(r"(^\d*)",ids).group(0))
        if id in BMID:
            print(link+": Map already exist")
            os.system('"'+os.path.abspath("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")+'" '+link)
            return
        if not id.isdigit():
            print("Invalid id")
            return
        cj = browser_cookie3.load()
        print("Downloading", link, "in", os.path.abspath(osupath+"/Songs/"))
        headers = {"referer": link}
        with requests.get(link) as r:
            t = BS(r.text, 'html.parser').title.text.split("·")[0]
        with requests.get(link+"/download", stream=True, cookies=cj, headers=headers) as r:
            if r.status_code == 200:
                try:
                    id = re.sub("[^0-9]", "", link)
                    with open(os.path.abspath(__file__.split("NewAltDownload.pyw")[0]+id+" "+t+".osz"), "wb") as otp:
                        otp.write(r.content)
                    shutil.copy(os.path.abspath(__file__.split("NewAltDownload.pyw")[0]+id+" "+t+".osz"),os.path.abspath(osupath+"/Songs/"+id+" "+t+".osz"))
                except:
                    print("You either aren't connected on osu!'s website or you're limited by the API, in which case you now have to wait 1h and then try again.")
    else:
        os.system('"'+os.path.abspath("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")+'" '+link)

args = sys.argv
if len(args) == 1:
    print("No arguments provided, exiting...")
    exit()
Prep_download(args)

你用 sys.argv()[-1] 获得了参数 %1 (link)(因为 sys.argv 是一个列表),然后从那里,你只需检查是否link 与您正在寻找的 link 相似(在我的例子中,它需要看起来像 https://osu.ppy.sh/b/https://osu.ppy.sh/beatmapsets/) 如果是这种情况,请执行一些代码,否则,只需使用 chrome 可执行文件和 link 作为参数启动 chrome。如果在 Songs 文件夹中找到了 beatmap 的 id,那么我也会在 chrome.

中打开 link

为了让它在后台工作,我不得不与子进程和更多技巧作斗争,最后,它突然开始使用 pythonw.pyw 扩展。