Running Xvfb and CutyCapt as Python subprocess
Asked Answered
S

1

5

I'm an trying to take a screenshot in the background using CutyCapt

My application is written in python and calls CutyCapt by running a subprocess.

Works locally (windows) just fine, but the CutyCapt.exe for windows does not require an x server. When I try to execute my code (via the python subprocess) on my ubuntu box, it barks about me not supplying a command to Xvfb. However, if I run the command on the box myself it works fine.

Command that works on box:

box$ xvfb-run --server-args="-screen 0, 1100x800x24" ./CutyCapt --url=http://www.google.com --out=temp.png

Python Code that fails:

def url_screengrab(url, **kwargs):
    url, temp_path, filename, url_hash = get_temp_screengrab_info(url)
    args = []
    if sys.platform.startswith("linux"):
        args.append('xvfb-run')
        args.append('--server-args="-screen 0, 1100x800x24"')
    args.append(settings.CUTYCAPT_EXE_PATH)
    args.append('--url=%s' % (url))
    args.append('--out=%s' % (temp_path,))
    subprocess.Popen(args, shell=False)
    return temp_path, filename, url_hash

Returned error:

xvfb-run: usage error: need a command to run
box$

Things I've tried: -using call instead of Popen -stripping the quote from the screen args -breaking the screen args up into a list -setting os.environ["DISPLAY"]=":0" before executing the subprocess

Do I need to break the xvfb process up from the CutyCapt command?

Any help would be greatly appreciated.

Sunil answered 15/4, 2012 at 1:27 Comment(4)
If you are using shell = True, then the first argument to Popen should be a string, not a list. When shell = False (the default), the first argument should be a list. Have you tried it with shell = False?Ollieollis
If settings.PLATFORM is not "linux", how is xvfb-run getting appended to args?Ollieollis
Instead of manually building args, try import shlex; args = shlex.split(''' xvfb-run --server-args="-screen 0, 1100x800x24" ./CutyCapt --url=http://www.google.com --out=temp.png ''').Ollieollis
@ubuntu - i dont need to run xvfb on windows (locally)Sunil
O
7

On Ubuntu 11.10, with the cutycapt and xvfb packages installed, the following works (at least for me...):

import shlex
import subprocess

def url_screengrab(url, **kwargs):
    cmd = '''xvfb-run --server-args "-screen 0, 1100x800x24"
             /usr/bin/cutycapt --url={u} --out=temp.png '''.format(u = url)
    proc = subprocess.Popen(shlex.split(cmd))
    proc.communicate()

url = 'http://www.google.com'
url_screengrab(url)
Ollieollis answered 15/4, 2012 at 16:11 Comment(3)
that works, thx. is there anything I have to do to guarantee shut down of the subprocess, as it seems my next line of code can't access the temp.png file even though it created it correctly?Sunil
proc.communicate() will block until the subprocess completes.Ollieollis
@Sunil can you please explain how did you shut down the subprocess?Willed

© 2022 - 2024 — McMap. All rights reserved.