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.

MattoTodd
  • 14,467
  • 16
  • 59
  • 76
  • 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`? – unutbu Apr 15 '12 at 01:46
  • If `settings.PLATFORM` is not `"linux"`, how is `xvfb-run` getting appended to `args`? – unutbu Apr 15 '12 at 01:47
  • 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 ''')`. – unutbu Apr 15 '12 at 01:48
  • @ubuntu - i dont need to run xvfb on windows (locally) – MattoTodd Apr 15 '12 at 02:08

1 Answers1

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)
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 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? – MattoTodd Apr 15 '12 at 16:44
  • `proc.communicate()` will block until the subprocess completes. – unutbu Apr 15 '12 at 16:49
  • 1
    @MattoTodd can you please explain how did you shut down the subprocess? – Javed Oct 06 '18 at 04:43