1

pyppeteer でクライアント証明書を使用してページにアクセスする必要があります。ここで puppeteer 自体で可能であることがわかりましたが、私の試みは python3 で無効な状態エラーにつながります。私の試みが完全に正しいかどうかはわかりませんが、操り人形師の問題コメントの精神に従っています。私はそれが可能であると期待しているので、私の試みはここにエラーがあります:

[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