推荐学习书目
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 · Oct 24, 2020 · 3465 views
    This topic created in 2025 days ago, the information mentioned may be changed or developed.

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

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

    我这边用了 mitmproxy 比较卡。

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

    10 replies    2020-10-25 11:12:24 +08:00
    locoz
        1
    locoz  
       Oct 24, 2020 via Android
    代理是最好的办法了…
    ClericPy
        2
    ClericPy  
       Oct 25, 2020
    ```
    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  
       Oct 25, 2020
    自己解析解密 chrome 的 sqlite3 格式 cookies 库(可能比想象的稍微难一点)或是用 browser-cookie3 https://pypi.org/project/browser-cookie3/
    mengyx
        5
    mengyx  
       Oct 25, 2020
    我用做过一个 Golang 的 MITM Proxy 。性能还行,外包的项目中实际验证过,需要的话可以找我
    mengyx
        6
    mengyx  
       Oct 25, 2020
    或者 Chrome 的 Devtools 也有相应 Api,https://chromedevtools.github.io/devtools-protocol/
    ClericPy
        7
    ClericPy  
       Oct 25, 2020
    V2 吞我空格...?

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

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

    https://github.com/kajweb/stop-debugger
    kajweb
        9
    kajweb  
       Oct 25, 2020
    浏览器拓展可以参考 XSwitch 的源码
    woshichuanqilz
        10
    woshichuanqilz  
    OP
       Oct 25, 2020
    解决了谢谢各位的帮助, 主要参考的是 @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)

    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3314 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 12:49 · PVG 20:49 · LAX 05:49 · JFK 08:49
    ♥ Do have faith in what you're doing.