Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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!

like image 533
Francis Michels Avatar asked Nov 05 '22 17:11

Francis Michels


1 Answers

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:]) 
like image 99
小子欠扁 Avatar answered Nov 15 '22 07:11

小子欠扁