Search code examples
pythonsubprocessxvfbcutycapt

Running Xvfb and CutyCapt as Python subprocess


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.


Solution

  • 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)