django python子进程- google-chrome headless -自签名证书-屏幕截图

vuktfyat  于 2023-04-22  发布在  Go
关注(0)|答案(1)|浏览(184)

打开Django shell:

python manage.py shell

然后输入:

import subprocess
command = [
  f'google-chrome',
  '--headless=new',
  f'--screenshot=/home/user/tmp/screen.png',
  f'--window-size=800,1700',
  '--no-first-run',
  '--allow-http-screen-capture',
  '--force-device-scale-factor=1',
  '--ignore-certificate-errors',
  '--ignore-urlfetcher-cert-requests',
  '--disable-test-root-certs',
  '--allow-running-insecure-content',
  '--hide-scrollbars',
  '--allow-insecure-localhost',
  '--disable-system-font-check',
  '--disable-gpu',
  '--disable-extensions',
  '--disable-dev-shm-usage',
  '--disable-software-rasterizer',
  '--disable-notifications',
  '--user-data-dir=/tmp/chromium-home',
  '/home/user/tmp/screen.html',
]
subprocess.run(command, **{})

控制台输出如下:

[32873:32901:0419/083534.872076:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/user/0/bus: Permission denied
[32873:32901:0419/083534.884135:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/user/0/bus: Permission denied
[32873:32901:0419/083534.970536:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/user/0/bus: Permission denied
[32873:32901:0419/083534.970630:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/user/0/bus: Permission denied

(process:32873): dconf-CRITICAL **: 08:35:35.289: unable to create directory '/run/user/0/dconf': Permission denied.  dconf will not work properly.

(process:32873): dconf-CRITICAL **: 08:35:35.314: unable to create directory '/run/user/0/dconf': Permission denied.  dconf will not work properly.

(process:32873): dconf-CRITICAL **: 08:35:35.350: unable to create directory '/run/user/0/dconf': Permission denied.  dconf will not work properly.
[32873:32899:0419/083537.631303:ERROR:cert_verify_proc_builtin.cc(679)] CertVerifyProcBuiltin for 127.0.0.1 failed:
----- Certificate i=0 (<snip>) -----
ERROR: No matching issuer found

[32873:32897:0419/083537.639022:ERROR:cert_verify_proc_builtin.cc(679)] CertVerifyProcBuiltin for 127.0.0.1 failed:
----- Certificate i=0 (<snip>) -----
ERROR: No matching issuer found

572051 bytes written to file /home/user/tmp/screen.png
CompletedProcess(args=['google-chrome', '--headless=new', '--screenshot=/home/user/tmp/screen.png', '--window-size=800, 1700', '--no-first-run', '--allow-http-screen-capture', '--force-device-scale-factor=1', '--ignore-certificate-errors', '--ignore-urlfetcher-cert-requests', '--disable-test-root-certs', '--allow-running-insecure-content', '--hide-scrollbars', '--allow-insecure-localhost', '--disable-system-font-check', '--disable-gpu', '--disable-extensions', '--disable-dev-shm-usage', '--disable-software-rasterizer', '--disable-notifications', '--user-data-dir=/tmp/chromium-home', '/home/user/tmp/screen.html'], returncode=0)

看起来令人担忧,但screen.png文件是正确的“截屏”。我们甚至可以看到证书错误,这些错误在子进程调用中被提到,但显然被忽略了。
顺便说一下,bash调用也可以工作,即:

$ google-chrome --headless=new --screenshot=/home/user/tmp/screen.png --window-size=800,1700 --no-first-run --allow-http-screen-capture --force-device-scale-factor=1 --ignore-certificate-errors --ignore-urlfetcher-cert-requests --disable-test-root-certs --allow-running-insecure-content --hide-scrollbars --allow-insecure-localhost --disable-system-font-check --disable-gpu --disable-extensions --disable-dev-shm-usage --disable-software-rasterizer --disable-notifications --user-data-dir=/tmp/chromium-home /home/user/tmp/screen.html

工作也。
Google Chrome版本是Rocky Linux 9上的google-chrome-unstable.x86_64 114.0.5696.0-1。下面描述的问题与稳定版本相同。
现在我想在模型函数中运行上面的子进程调用,如下所示:

class MyModel(models.Model):
    <snip>

    def render(self):
        import subprocess
        commands = [
            f'google-chrome',
            '--headless=new',
            f'--screenshot=/home/user/tmp/screen.png',
            f'--window-size=800,1700',
            '--no-first-run',
            '--allow-http-screen-capture',
            '--force-device-scale-factor=1',
            '--ignore-certificate-errors',
            '--ignore-urlfetcher-cert-requests',
            '--disable-test-root-certs',
            '--allow-running-insecure-content',
            '--hide-scrollbars',
            '--allow-insecure-localhost',
            '--disable-system-font-check',
            '--disable-gpu',
            '--disable-extensions',
            '--disable-dev-shm-usage',
            '--disable-software-rasterizer',
            '--disable-notifications',
            '--user-data-dir=/tmp/chromium-home',
            '/home/user/tmp/screen.html',
        ]
        subprocess.run()

命令行输出为:

[33681:33708:0419/085053.463656:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/user/0/bus: Permission denied
[33681:33708:0419/085053.464599:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/user/0/bus: Permission denied
[33681:33708:0419/085053.589267:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/user/0/bus: Permission denied
[33681:33708:0419/085053.589373:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/user/0/bus: Permission denied

(process:33681): dconf-CRITICAL **: 08:50:53.761: unable to create directory '/run/user/0/dconf': Permission denied.  dconf will not work properly.

(process:33681): dconf-CRITICAL **: 08:50:53.768: unable to create directory '/run/user/0/dconf': Permission denied.  dconf will not work properly.

(process:33681): dconf-CRITICAL **: 08:50:53.821: unable to create directory '/run/user/0/dconf': Permission denied.  dconf will not work properly.
8899 bytes written to file /home/user/tmp/screen.png

错误是类似的,但SSL证书错误不存在。
假设HTML文件的内容是(或非常相似,文件是动态生成的):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no">
<style>
*{
    box-sizing:border-box;
    margin:0;
    padding:0;
}
html{
    -webkit-print-color-adjust:exact;
}
.upload{
    overflow:hidden;
    height:1600px;
    width:800px;
    display:block;
}
.upload > img{
    display:block;
    width:100%;
}
</style>
</head>
<body>
<div class="upload">
<img src="https://docs.celeryq.dev/en/stable/_static/celery_512.png"><br>
<img src="https://127.0.0.1:1310/m/u/befe5aa9-690b-410d-a3bc-7ba9a75c382a.webp" alt="This is a test">
</div>
</body>
</html>

我猜subnprocess是以某种方式跳过?忽略?丢弃?由无头浏览器进行的图像调用?
一句话:

  • 从bash命令行工作
  • 从“正常”python shell工作
  • 不能从对象方法工作

我很困惑--有什么线索吗?

r1zk6ea1

r1zk6ea11#

如果你原谅我的法语,我是一个白痴:)将为后代发布答案,因为互联网是永远的,作为对自己的提醒。|
答案就在HTML文件的动态生成中:
破损版本:

def render(self):
     html_render = '<some /><markup />'
     with open(temp_html_full_path, 'w') as f:
         f.write(html_render)
         command = []
         subprocess.run(command, **{})

工作版本:

def render(self):
     html_render = '<some /><markup />'
     with open(temp_html_full_path, 'w') as f:
         f.write(html_render)
     command = []
     subprocess.run(command, **{})

注意subprocess调用是如何在with文件写入块内部/外部进行的。

相关问题