簡體   English   中英

sh.py沒有執行xvfb運行

[英]sh.py not executing xvfb-run

我正在嘗試通過py.sh執行xvfb-run ,但我正在獲取sh.ErrorReturnCode_1並且沒有創建結果pdf。

我創建了一個小的html文件:

$ echo '<h1>Hello, World.</h1>' > test.html

然后,我在Python中通過sh.py運行了xvfb_run

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sh import xvfb_run
>>> xvfb_run('--server-num=1 --server-args="-screen 0, 1024x768x24" '
...     '/usr/bin/wkhtmltopdf --ignore-load-errors', 'test.html', 'test.pdf')
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/mark/.virtualenvs/reports/local/lib/python2.7/site-packages/sh.py", line 726, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/home/mark/.virtualenvs/reports/local/lib/python2.7/site-packages/sh.py", line 291, in __init__
    self.wait()
  File "/home/mark/.virtualenvs/reports/local/lib/python2.7/site-packages/sh.py", line 295, in wait
    self._handle_exit_code(self.process.wait())
  File "/home/mark/.virtualenvs/reports/local/lib/python2.7/site-packages/sh.py", line 309, in _handle_exit_code
    self.process.stderr
sh.ErrorReturnCode_1: 

  RAN: '/usr/bin/xvfb-run --server-num=1 --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf --ignore-load-errors test.html test.pdf'

  STDOUT:


  STDERR:

然后,我在shell中重新查看是否已創建任何東西,但沒有任何東西:

$ ls -l
total 4
-rw-rw-r-- 1 mark mark 23 May 22 07:54 test.html

因此,我從上面復制了xvfb-run命令,它可以正常工作:

$ /usr/bin/xvfb-run --server-num=1 --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf --ignore-load-errors test.html test.pdf
Loading page (1/2)
Printing pages (2/2)                                               
Done                                                           

還有我嘗試創建的PDF文件:

$ ls -l
total 12
-rw-rw-r-- 1 mark mark   23 May 22 07:54 test.html
-rw-rw-r-- 1 mark mark 7091 May 22 07:55 test.pdf

然后,我嘗試使用標准庫中的call方法:

>>> from subprocess import call
>>> call(['/usr/bin/xvfb-run', '--server-num=1', '--server-args="-screen 0, 1024x768x24"', '/usr/bin/wkhtmltopdf', '--ignore-load-errors', 'test.html', 'test.pdf'])
xvfb-run: error: Xvfb failed to start
1

然后,我認為未設置DISPLAY環境var,但我對此也沒有任何高興:

>>> import os
>>> os.environ["DISPLAY"]=":99"
>>> call(['/usr/bin/xvfb-run', '--server-num=1', '--server-args="-screen 0, 1024x768x24"', '/usr/bin/wkhtmltopdf', '--ignore-load-errors', 'test.html', 'test.pdf'])
xvfb-run: error: Xvfb failed to start
1

>>> call(['/usr/bin/wkhtmltopdf', '--ignore-load-errors', 'test.html', 'test.pdf'])
wkhtmltopdf: cannot connect to X server :99
1

>>> os.environ["DISPLAY"]=":1"
>>> call(['/usr/bin/wkhtmltopdf', '--ignore-load-errors', 'test.html', 'test.pdf'])
wkhtmltopdf: cannot connect to X server :1

知道為什么py.sh和call無法運行該命令嗎? 我在這里想念什么嗎?

我需要分別運行/usr/bin/Xvfb :1 -screen 0 1024x768x24並使它在后台運行。 然后就不需要xvfb-run ,我可以wkhtmltopdf地執行wkhtmltopdf

>>> import os
>>> os.environ["DISPLAY"]=":1"
>>> from sh import wkhtmltopdf
>>> wkhtmltopdf('--ignore-load-errors', 'test.html', 'test.pdf')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM