Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

manage.py: cannot connect to X server

I have used PyQt4.QtWebkit to crawl the web page in my django application.In the production environment that module doesn't work to crawl it.it throws the error "manage.py: cannot connect to X server"

My Qt class :

class Render(QWebPage):
 def __init__(self, url):
    self.app = QApplication(sys.argv)
    QWebPage.__init__(self)
    self.loadFinished.connect(self._loadFinished)
    self.mainFrame().load(QUrl(url))
    self.app.exec_()

 def _loadFinished(self, result):
    self.frame = self.mainFrame()
    self.app.quit() 

calling from django-shell:

 r = Render(url)

when i call this "Render" class through django with the Django-shell(python manage.py shell) the render function throws the error. could you please help me on this?

like image 714
Nava Avatar asked Jan 18 '23 00:01

Nava


2 Answers

The Reason is "Xvfb"

i need to run my python program in bash shell with xvfb(X virtual frame buffer) likewise,

ubuntu@localhost$ xvfb-run python webpage_scrapper.py http://www.google.ca/search?q=navaspot

It gives the result.

Now My requirement is i need to execute this shell command in python and waiting for tine to collect the result.I have to process the result.

Could you please suggest me for executing this command on python effectively.

like image 78
Nava Avatar answered Jan 20 '23 13:01

Nava


Seems like environment variables for X display are not set and that's the reason you get such error. It can occur because you're running script from environment, that isn't bound to X display (ssh to server).

Try adding display variable:

DISPLAY=:0.0 python manage.py script

It is also possible to set DISPLAY environment variable from python. You may set it before calling the PyQt4:

import os
os.putenv('DISPLAY', ':0.0')

It's also may not be possible to run PyQt4.QtWebkit if your production environment doesn't have X server running.

like image 37
Marius Grigaitis Avatar answered Jan 20 '23 12:01

Marius Grigaitis