1

我需要使用带有 pyppeteer 的客户端证书访问页面,我在这里看到 puppeteer 本身是可能的,但是我的尝试导致 python3 出现无效状态错误。我不确定我的尝试是否完全正确,但它遵循 puppeteer 中问题评论的精神。我希望它是可能的,因此我的尝试是错误:

[E:pyppeteer.connection] connection unexpectedly closed
Task exception was never retrieved
future: <Task finished coro=<Connection._async_send() done, defined at /home/data/experim/jaenv/lib/python3.6/site-packages/pyppeteer/connection.py:69> exception=InvalidStateError('invalid state',)>
Traceback (most recent call last):
  File "/home/data/experim/jaenv/lib/python3.6/site-packages/pyppeteer/connection.py", line 73, in _async_send
    await self.connection.send(msg)
  File "/home/data/experim/jaenv/lib/python3.6/site-packages/websockets/protocol.py", line 467, in send
    yield from self.write_frame(True, OP_TEXT, data.encode('utf-8'))
  File "/home/data/experim/jaenv/lib/python3.6/site-packages/websockets/protocol.py", line 919, in write_frame
    yield from self.ensure_open()
  File "/home/data/experim/jaenv/lib/python3.6/site-packages/websockets/protocol.py", line 646, in ensure_open
    ) from self.transfer_data_exc
websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/data/experim/jaenv/lib/python3.6/site-packages/pyppeteer/connection.py", line 79, in _async_send
    await self.dispose()
  File "/home/data/experim/jaenv/lib/python3.6/site-packages/pyppeteer/connection.py", line 170, in dispose
    await self._on_close()
  File "/home/data/experim/jaenv/lib/python3.6/site-packages/pyppeteer/connection.py", line 153, in _on_close
    f'Protocol error {cb.method}: Target closed.',  # type: ignore
asyncio.base_futures.InvalidStateError: invalid state

这是导致上述错误的尝试:

import asyncio
from pyppeteer import launch
from pyppeteer.network_manager  import Request
import requests

async def interceptor(interceptReq, cert,key):
    res = requests.get(interceptReq.url , cert=('../../widshared/certexp.cer','../../widshared/certpriv.pem'))
    if not res.ok:
        return await interceptReq.abort('connectionrefused')
    else:
        return await interceptReq.respond({
             'status': res.status_code,
             'headers': res.headers,
             'body': res.content
             })

async def main():
    browser = await launch(headless = True )
    page = await browser.newPage()
    await page.setRequestInterception(True)
    with open("../../widshared/certexp.cer", 'r') as cr:
        cert = cr.read()
    with open("../../widshared/certpriv.pem", 'r') as cr:
        key = cr.read()
    page.on('request', lambda interceptReq: asyncio.ensure_future(interceptor(interceptReq, cert, key )) )
    await page.goto('https://client.badssl.com')
    await page.screenshot({'path': 'clientbadssl.png'})
    await browser.close()

asyncio.get_event_loop().run_until_complete(main())
4

0 回答 0