V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
woshichuanqilz
V2EX  ›  Python

Chrome 如何快速获取 request header?

  •  
  •   woshichuanqilz · 2020-10-24 23:05:23 +08:00 · 2731 次点击
    这是一个创建于 1251 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一般的做法就是 f12 打开 dev tool 然后复制粘贴,

    但是现在想用程序自动化的实现这个功能

    我这边用了 mitmproxy 比较卡。

    然后想到了 tampermonkey, 不太熟悉 js 没有找到办法, 这里请问一下如何能实现这个功能。 通过后台的程序自动化的获取 request header

    10 条回复    2020-10-25 11:12:24 +08:00
    locoz
        1
    locoz  
       2020-10-24 23:19:25 +08:00 via Android
    代理是最好的办法了…
    ClericPy
        2
    ClericPy  
       2020-10-25 00:09:18 +08:00
    ```
    import asyncio
    from ichrome import AsyncChromeDaemon, AsyncTab


    async def show_headers(tab: AsyncTab):
    await tab.wait_response(filter_function=lambda r: 'httpbin.org/headers' in
    r['params']['response']['url'],
    callback_function=lambda r: print(
    r['params']['response']['headers'], '\n', r[
    'params']['response']['requestHeaders']))
    # await tab.wait_request(lambda r: print(r))


    async def main():
    async with AsyncChromeDaemon() as cd:
    async with cd.connect_tab() as tab:
    task = asyncio.create_task(show_headers(tab))
    await tab.goto('https://httpbin.org/headers')
    # print(await tab.html)
    await task


    if __name__ == "__main__":
    asyncio.run(main())


    ```
    yucongo
        3
    yucongo  
       2020-10-25 00:22:02 +08:00
    自己解析解密 chrome 的 sqlite3 格式 cookies 库(可能比想象的稍微难一点)或是用 browser-cookie3 https://pypi.org/project/browser-cookie3/
    crab
        4
    crab  
       2020-10-25 00:30:57 +08:00
    mengyx
        5
    mengyx  
       2020-10-25 00:34:01 +08:00
    我用做过一个 Golang 的 MITM Proxy 。性能还行,外包的项目中实际验证过,需要的话可以找我
    mengyx
        6
    mengyx  
       2020-10-25 00:37:15 +08:00
    或者 Chrome 的 Devtools 也有相应 Api,https://chromedevtools.github.io/devtools-protocol/
    ClericPy
        7
    ClericPy  
       2020-10-25 00:49:51 +08:00
    V2 吞我空格...?

    https://paste.ubuntu.com/p/QrDmcGwxvS/

    加上 Headless 和禁用图片, 也可以屏蔽 css 和 mp4 没放上, 冷启动 3 秒, 连接已经启动的 tab 大概 1 秒(基本就是花在下载), 同域名并发被 Chrome 限制在 6 以内, 所以没写并发的
    kajweb
        8
    kajweb  
       2020-10-25 01:18:41 +08:00
    我最近准备开源的 stop-debugger 您可以关注一下,基于 node 屏蔽浏览器调试 debugger 。
    里面的 proxy-serve 模块可以满足您的要求。
    基本原理是使用 net 模块获得请求并进行代理。

    https://github.com/kajweb/stop-debugger
    kajweb
        9
    kajweb  
       2020-10-25 01:19:39 +08:00
    浏览器拓展可以参考 XSwitch 的源码
    woshichuanqilz
        10
    woshichuanqilz  
    OP
       2020-10-25 11:12:24 +08:00
    解决了谢谢各位的帮助, 主要参考的是 @Cleric 的思路, 我用了 pychrome

    基本代码在这里比较粗糙

    ```
    import pychrome
    from urllib.parse import urlparse
    import subprocess, signal
    import os
    import time


    def killprocess(pname):
    p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
    out, err = p.communicate()
    for line in out.splitlines():
    pinfo = line.decode().lower()
    if pname in pinfo:
    pid = int(line.split(None, 1)[0])
    os.kill(pid, signal.SIGKILL)

    header = dict()
    url = "https://www.dogedoge.com"
    if not url.endswith('/'):
    url += '/'
    domain = urlparse(url).netloc
    killprocess('chrome')
    cmd = 'google-chrome-stable --remote-debugging-port=9222'
    p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
    time.sleep(3)

    # 创建一个浏览器实例
    browser = pychrome.Browser(url="http://127.0.0.1:9222")

    # 新建一个页签
    tab = browser.new_tab()


    # 需要注册的回调函数
    def request_will_be_sent(**kwargs):
    if url == kwargs.get('request').get('url'):
    header = kwargs.get('request').get('headers')
    return


    tab.Network.requestWillBeSent = request_will_be_sent

    # 开始接收消息, requestWillBeSent 事件发生时被注册的回调函数也可以执行
    tab.start()

    # 调用方法
    tab.Network.enable()

    # 调用方法并设置超时时间
    tab.Page.navigate(url=url, _timeout=5)
    input()
    # 等待页面加载
    tab.wait(5)

    # 停止处理事件, 停止从 chrome 接收消息
    tab.stop()

    # 关闭页签
    browser.close_tab(tab)

    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 07:07 · PVG 15:07 · LAX 00:07 · JFK 03:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.