开发者

Error "Cannot connect to X server" with Python program webkit2png

开发者 https://www.devze.com 2023-03-24 18:13 出处:网络
When I use webkit2png on my SSH-server, I get the error: \"Cannot connect to X server:0.0\" Webkit2png is a package that makes a PNG screenshots of htmlpages.

When I use webkit2png on my SSH-server, I get the error: "Cannot connect to X server:0.0" Webkit2png is a package that makes a PNG screenshots of htmlpages. On my computer it worked perfectly, but on the SSH server not; it doens't create an PNG-image.

It only works when I do this command:

xvfb-run -a -s "-screen 0 640x480x16" python webkit2pngProgram.py

But that's an Unix command, and not Python code.

The webkit2pngProgram code:

from webkit2png import WebkitRenderer, init_qtgui
from PyQt4.QtCore import QTimer

def renderer_func():
   renderer = WebkitRenderer()
   renderer.width = 550
   renderer.height = 500   
   renderer.timeout = 10
   renderer.wait = 1
   renderer.format = "png"
   renderer.grabWholeWindow = False
   y = Program()
   outfile = open("/image.png", 开发者_开发知识库"w")

   renderer.render_to_file("/test.html", file=outfile)
   outfile.close()

def start():
   app = init_qtgui()   ###### => STUCKS HERE
   QTimer.singleShot(0, renderer_func)
   app.exec_()

if __name__ == "__main__":
    start()

Thanks for your help!


You need to run the xvfb unix command first (X Virtual Frame Buffer)

server_num = int(os.getpid() + 1e6) 
newArgs = ["xvfb-run", "--auto-servernum", "--server-num", str(server_num), "--server-args=-screen 0, %dx%dx24" % (1024, 768), sys.argv[0], '-g','1024', '768'] 
os.execvp(newArgs[0],newArgs[1:]) 
0

精彩评论

暂无评论...
验证码 换一张
取 消