Ghost.py and proxy

2019-06-08 08:29发布

问题:

Is anyone able to explain how to make Ghost.py work with a proxy? I've checked out the code but it's not there.

回答1:

I've found it in the ghost.py file. They did a very good job in it. It's a method on line 835, as set_proxy(). It's just how to use it that I'm yet to try out:

def set_proxy(self, type_, host='localhost', port=8888, user='',
        password=''):
    """Set up proxy for FURTHER connections.

    :param type_: proxy type to use: \
        none/default/socks5/https/http.
    :param host: proxy server ip or host name.
    :param port: proxy port.
    """
    _types = {
        'default': QNetworkProxy.DefaultProxy,
        'none': QNetworkProxy.NoProxy,
        'socks5': QNetworkProxy.Socks5Proxy,
        'https': QNetworkProxy.HttpProxy,
        'http': QNetworkProxy.HttpCachingProxy
    }

    if type_ is None:
        type_ = 'none'
    type_ = type_.lower()
    if type_ in ['none', 'default']:
        self.manager.setProxy(QNetworkProxy(_types[type_]))
        return
    elif type_ in _types:
        proxy = QNetworkProxy(_types[type_], hostName=host, port=port,
            user=user, password=password)
        self.manager.setProxy(proxy)
    else:
        raise ValueError('Unsupported proxy type:' + type_ \
        + '\nsupported types are: none/socks5/http/https/default')

What I don't understand now is what "QNetworkProxy.DefaultProxy" means. It's said to be the default proxy. So, what's the default proxy?



回答2:

If in Ghost.py the way of making tcp connections based on Qt api, then you may use Qt/PySide api, see QNetworkProxy::setApplicationProxy(). Otherwise, if Ghost.py not using Qt Api, but for example curl libe, then you try to set environment variable "http_proxy"



回答3:

Documentation says that QNetworkProxy.DefaultProxy: Proxy is determined based on the application proxy set using setApplicationProxy() So if proxy is set by QNetworkProxy::setApplicationProxy(), then the call set_proxy('default') will make to use it (it will pass the proxy to self.manager, which I guess is the QNetworkAccessManager object).



回答4:

You can use below code. It works for me,

from ghost import Ghost, Session
ghost = Ghost()
with ghost.start():
    session = Session(ghost)
    session.wait_timeout = 999
    session.set_proxy('http', str(ip), int(port), str(username), str(password))
    page, resource = session.open(url)
    print session.content # Prints html content
    print page.headers, page.url, page.http_status

The ghost object has only one method i.e start(). Rest of them are defined as methods of Session class.