在openshift上使用python,selenium和phantomjs(套接字绑定权限被拒绝?)

时间:2016-06-27 00:41:30

标签: python-3.x selenium-webdriver phantomjs openshift

好吧,我正在我的系绳尽头试图让phantomJS在开放式环境中使用硒。我已经使用ssh下载了phantomjs二进制文件,甚至可以在shell中运行它。但是当谈到使用selenium启动webdriver服务时,无论我输入的args如何,我都会收到此跟踪错误。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/lib/openshift/576e22027628e1fb13000211/python/virtenv/venv/lib/python3.3/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 50, in __init__
    service_args=service_args, log_path=service_log_path)
  File "/var/lib/openshift/576e22027628e1fb13000211/python/virtenv/venv/lib/python3.3/site-packages/selenium/webdriver/phantomjs/service.py", line 50, in __init__
    service.Service.__init__(self, executable_path, port=port, log_file=open(log_path, 'w'))
  File "/var/lib/openshift/576e22027628e1fb13000211/python/virtenv/venv/lib/python3.3/site-packages/selenium/webdriver/common/service.py", line 33, in __init__
    self.port = utils.free_port()
  File "/var/lib/openshift/576e22027628e1fb13000211/python/virtenv/venv/lib/python3.3/site-packages/selenium/webdriver/common/utils.py", line 36, in free_port
    free_socket.bind(('0.0.0.0', 0))

PermissionError: [Errno 13] Permission denied

不确定发生了什么,我应该绑定到IP地址吗?如果是这样,我尝试使用服务args,但这没有帮助。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,试图在我的Openshift托管的Django应用程序上运行phantomJS,运行在Python 3设备上。最后我设法让它工作,这是如何:

要克服的主要问题是Openshift不允许应用程序绑定到localhost(也不是&0; 0.0.0.0&#39;也不是&#39; 127.0.0.1)。所以重点是绑定到您的Openshift Gear的实际IP地址

您必须在ghostdriver级别以及Python-selenium绑定中处理此问题。

ghostdriver(phantomJS二进制)

不幸的是,正如Paolo Bernardi在这篇文章中所解释的那样出色:http://www.bernardi.cloud/2015/02/25/phantomjs-with-ghostdriver-on-openshift/你必须使用补丁版本的phantomjs,因为发布的版本不允许绑定到指定的IP。由Paolo链接的二进制文件在我的Python3卡片上不起作用,但这个完美无缺:https://github.com/jrestful/server/blob/master/seo/phantomjs-1.9.8-patched.tar.gz?raw=true (详见问题Trying to run PhantomJS on OpenShift: cannot patch GhostDriver so that it can bind on the server IP address

将此phantomjs二进制文件上传到app-root / data / phantomjs / bin(例如),并确保它可以运行:

> chmod 711 app-root/data/phantomjs/bin/phantomjs

您现在可以检查是否可以像这样容纳您的IP(我为我的应用选择了端口号15002,我估计您可以选择15000以上的任何值):

> echo $OPENSHIFT_PYTHON_IP
127.13.XXX.XXX
> app-root/data/phantomjs/bin/phantomjs --webdriver=127.13.XXX.XXX:15002
PhantomJS is launching GhostDriver...
[INFO  - 2017-03-24T13:16:36.031Z] GhostDriver - Main - running on port 127.13.XXX.XXX:15002

好的,现在终止这个过程并继续执行第2步:python webdriver

用于PhantomJS的自定义python-selenium webdriver

重点是添加要绑定的IP地址作为PhantomJS Webdriver的参数。

首先,我在settings.py中定义了新设置以适应Openshift的约束

PHANTOMJS_BIN_PATH = os.path.join(os.getenv('OPENSHIFT_DATA_DIR'), 'phantomjs', 'bin', 'phantomjs')
PHANTOMJS_LOG_PATH = os.path.join(os.getenv('OPENSHIFT_LOG_DIR'), 'ghostdriver.log')

(确保app-root / logs /是可写的,也许你必须chmod它)

然后,我不得不重写PhantomJS Webdriver类,提供ip地址作为参数。这是我自己的实现:

from selenium.webdriver import phantomjs
from selenium.webdriver.common import utils
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class MyPhantomJSService(phantomjs.service.Service):
    def __init__(self, executable_path, port=0, service_args=None, log_path=None, ip=None):
        if ip is None:
            self.ip = '0.0.0.0'
        else:
            self.ip = ip
        phantomjs.service.Service.__init__(self, executable_path, port, service_args, log_path)

    def command_line_args(self):
        return self.service_args + ["--webdriver=%s:%d" % (self.ip, self.port)]

    def is_connectable(self):
        return utils.is_connectable(self.port, host=self.ip)

    @property
    def service_url(self):
        """
        Gets the url of the GhostDriver Service
        """
        return "http://%s:%d/wd/hub" % (self.ip, self.port)


class MyPhantomWebDriver(RemoteWebDriver):
    """
    Wrapper to communicate with PhantomJS through Ghostdriver.

    You will need to follow all the directions here:
    https://github.com/detro/ghostdriver
    """

    def __init__(self, executable_path="phantomjs",
                 ip=None, port=0, desired_capabilities=DesiredCapabilities.PHANTOMJS,
                 service_args=None, service_log_path=None):
        """
        Creates a new instance of the PhantomJS / Ghostdriver.

        Starts the service and then creates new instance of the driver.

        :Args:
         - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
         - ip - IP sur lequel on veut se binder : c'est la spécificité de ce monkeypatch
         - port - port you would like the service to run, if left as 0, a free port will be found.
         - desired_capabilities: Dictionary object with non-browser specific
           capabilities only, such as "proxy" or "loggingPref".
         - service_args : A List of command line arguments to pass to PhantomJS
         - service_log_path: Path for phantomjs service to log to.
        """
        self.service = MyPhantomJSService(
            executable_path,
            port=port,
            service_args=service_args,
            log_path=service_log_path,
            ip=ip)
        self.service.start()

        try:
            RemoteWebDriver.__init__(
                self,
                command_executor=self.service.service_url,
                desired_capabilities=desired_capabilities)
        except Exception:
            self.quit()
            raise

        self._is_remote = False

    def quit(self):
        """
        Closes the browser and shuts down the PhantomJS executable
        that is started when starting the PhantomJS
        """
        try:
            RemoteWebDriver.quit(self)
        except Exception:
            # We don't care about the message because something probably has gone wrong
            pass
        finally:
            self.service.stop()

最后,调用这个自定义webdriver而不是webdriver.PhantomJS(......,就像这样:

from .myphantomjs import MyPhantomWebDriver

browser = MyPhantomWebDriver(executable_path=settings.PHANTOMJS_BIN_PATH, service_log_path=settings.PHANTOMJS_LOG_PATH, ip=os.getenv('OPENSHIFT_PYTHON_IP'), port=15002)

从那时起,您可以正常使用浏览器对象