Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

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.

Generally on headless machines, the DISPLAY variable is absent or misconfigured. To work on such machines, you can use the following approach. As a example for Ubuntu 14.04-LTS machines:

First install X server: sudo apt-get install xserver-xorg

Now start the X server (say at :0): sudo /usr/bin/X :0&

You can use process managers like supervisor to handle the above process.

Now just set the DISPLAY environment variable and make sure it is available to any processes you are running which depend on this, DISPLAY=:0 python manage.py

The way you provide the environment variables to your application is upto you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top