Beispiel #1
0
 def raise_error():
     # failure.parents[-1] will be the exception class for local
     # failures and the string name of the exception class
     # for remote failures (which might not exist in our
     # namespace)
     #
     # failure.value will be the tuple of arguments to the
     # exception in the local case, or a string
     # representation of that in the remote case (see
     # pb.CopyableFailure.getStateToCopy()).
     #
     # we can only reproduce a remote exception if the
     # exception class is in our namespace, and it only takes
     # one string argument. if either condition is not true,
     # we wrap the strings in a default Exception.
     if common.versionStringToTuple(version.short()) >= (11, 1, 0):
         k, v = failure.parents[0], failure.value
     else:
         k, v = failure.parents[-1], failure.value
     try:
         if isinstance(k, str):
             k = reflect.namedClass(k)
         if isinstance(v, tuple):
             e = k(*v)
         else:
             e = k(v)
     except Exception:
         e = Exception('%s: %r' % (failure.type, v))
     raise e
Beispiel #2
0
def main(reactor, url):
    """
    We create a custom UserAgent and send a GET request to a web server.
    """
    url = url.encode("ascii")
    userAgent = "Twisted/{} (httpclient.py)".format(
        version.short()).encode("ascii")
    agent = Agent(reactor)
    d = agent.request(b"GET", url, Headers({b"user-agent": [userAgent]}))

    def cbResponse(response):
        """
        Prints out the response returned by the web server.
        """
        pprint(vars(response))
        proto = WriteToStdout()
        if response.length is not UNKNOWN_LENGTH:
            print("The response body will consist of", response.length,
                  "bytes.")
        else:
            print("The response body length is unknown.")
        response.deliverBody(proto)
        return proto.onConnLost

    d.addCallback(cbResponse)
    d.addErrback(log.err)
    d.addBoth(lambda ign: reactor.callWhenRunning(reactor.stop))
    reactor.run()
Beispiel #3
0
def main(reactor, url):
    """
    We create a custom UserAgent and send a GET request to a web server.
    """
    userAgent = 'Twisted/%s (httpclient.py)' % (version.short(), )
    agent = Agent(reactor)
    d = agent.request('GET', url, Headers({'user-agent': [userAgent]}))

    def cbResponse(response):
        """
        Prints out the response returned by the web server.
        """
        pprint(vars(response))
        proto = WriteToStdout()
        if response.length is not UNKNOWN_LENGTH:
            print('The response body will consist of', response.length,
                  'bytes.')
        else:
            print('The response body length is unknown.')
        response.deliverBody(proto)
        return proto.onConnLost

    d.addCallback(cbResponse)
    d.addErrback(log.err)
    d.addBoth(lambda ign: reactor.callWhenRunning(reactor.stop))
    reactor.run()
Beispiel #4
0
    def __init__(self):
        self.methods = {
            'PRIVMSG': self.msg,
            'ACTION': self.me,
            'NOTICE': self.notice,
            'TOPIC': self.topic,
            'RESET': self.reimport,
            'MULTIPLE': self.multiple,
        }
        self.chanmodes = {}
        self.whoreplies = {}
        self.hostmask = ''  # until we see ourselves speak, we do not know
        self.servername = ''
        self.lineRate = 1

        self.versionName = 'Yardbird'
        self.versionNum = VERSION
        udata = uname()
        self.versionEnv = 'Twisted %s and Django %s on %s-%s' % \
                (twisted_version.short(), django_version(), udata[0],
                 udata[4])
        self.sourceURL = 'http://zork.net/~nick/yardbird/ '
        self.realname = 'Charlie Parker Jr.'
        self.fingerReply = str(settings.INSTALLED_APPS)
        self.l = task.LoopingCall(self.PING)
Beispiel #5
0
    def __init__(self):
        self.methods = {
            "PRIVMSG": self.msg,
            "ACTION": self.me,
            "NOTICE": self.notice,
            "TOPIC": self.topic,
            "RESET": self.reimport,
            "MULTIPLE": self.multiple,
        }
        self.chanmodes = {}
        self.whoreplies = {}
        self.hostmask = ""  # until we see ourselves speak, we do not know
        self.servername = ""
        self.lineRate = 1

        self.versionName = "Yardbird"
        self.versionNum = VERSION
        udata = uname()
        self.versionEnv = "Twisted %s and Django %s on %s-%s" % (
            twisted_version.short(),
            django_version(),
            udata[0],
            udata[4],
        )
        self.sourceURL = "http://zork.net/~nick/yardbird/ "
        self.realname = "Charlie Parker Jr."
        self.fingerReply = str(settings.INSTALLED_APPS)
        self.l = task.LoopingCall(self.PING)
Beispiel #6
0
 def setCommonHeaders(self):
     """Get the common HTTP headers for all requests."""
     headers = http_headers.Headers()
     headers.setHeader('Host', self.host)
     headers.setHeader('User-Agent', 'apt-p2p/%s (twisted/%s twisted.web2/%s)' % 
                       (version.short(), twisted_version.short(), web2_version.short()))
     return headers
Beispiel #7
0
def main(reactor, url):
    userAgent = 'Twisted/%s (httpclient.py)' % (version.short(),)
    agent = Agent(reactor)
    d = agent.request(
        'GET', url, Headers({'user-agent': [userAgent]}))
    def cbResponse(response):
        pprint(vars(response))
        proto = WriteToStdout()
        if response.length is not UNKNOWN_LENGTH:
            print 'The response body will consist of', response.length, 'bytes.'
        else:
            print 'The response body length is unknown.'
        response.deliverBody(proto)
        return proto.onConnLost
    d.addCallback(cbResponse)
    d.addErrback(log.err)
    d.addBoth(lambda ign: reactor.callWhenRunning(reactor.stop))
    reactor.run()
Beispiel #8
0
    def __init__(self):
        self.methods = {'PRIVMSG':  self.msg,
                        'ACTION':   self.me,
                        'NOTICE':   self.notice,
                        'TOPIC':    self.topic,
                        'RESET':    self.reimport,
                       }
        self.chanmodes = {}
        self.whoreplies = {}
        self.hostmask = '' # until we see ourselves speak, we do not know
        self.servername = ''
        self.lineRate = 1

        self.versionName = 'Yardbird'
        self.versionNum = VERSION
        udata = uname()
        self.versionEnv = 'Twisted %s and Django %s on %s-%s' % \
                (twisted_version.short(), django_version(), udata[0],
                 udata[4])
        self.sourceURL = 'http://zork.net/~nick/yardbird/ '
        self.realname = 'Charlie Parker Jr.'
        self.fingerReply = str(settings.INSTALLED_APPS)
Beispiel #9
0
    def test_pinned_twisted_version(self):
        """When running tests within a Tox environment with pinned
        dependencies, make sure that the version of Twisted is the pinned
        version.

        See https://github.com/scrapy/scrapy/pull/4814#issuecomment-706230011
        """
        if not os.environ.get('_SCRAPY_PINNED', None):
            self.skipTest('Not in a pinned environment')

        tox_config_file_path = os.path.join(
            os.path.dirname(__file__),
            '..',
            'tox.ini',
        )
        config_parser = ConfigParser()
        config_parser.read(tox_config_file_path)
        pattern = r'Twisted==([\d.]+)'
        match = re.search(pattern, config_parser['pinned']['deps'])
        pinned_twisted_version_string = match[1]

        self.assertEqual(twisted_version.short(),
                         pinned_twisted_version_string)
Beispiel #10
0
ARlIjNvrPq86fpVg0NOTawALkSqOUMl3MynBQO+spR7EHcRbADQ/JemfTEh2Ycfl
vZqhEFBfurZkX0eTANq98ZvVfpg=
-----END CERTIFICATE-----"""))

# `t.w.client.HTTPConnectionPool` isn't available in Twisted-12.0.0
# (see ticket #11219: https://bugs.torproject.org/11219):
_connectionPoolAvailable = _twistedversion >= Version('twisted', 12, 1, 0)
if _connectionPoolAvailable:
    logging.info("Using HTTPConnectionPool for reCaptcha API server.")
    _pool = client.HTTPConnectionPool(reactor, persistent=False)
    _pool.maxPersistentPerHost = 5
    _pool.cachedConnectionTimeout = 30
    _agent = client.Agent(reactor, pool=_pool)
else:
    logging.warn("Twisted-%s is too old for HTTPConnectionPool! Disabling..."
                 % _twistedversion.short())
    _pool = None
    _agent = client.Agent(reactor)


# Twisted>=14.0.0 changed the way in which hostname verification works.
if _twistedversion >= Version('twisted', 14, 0, 0):
    from twisted.internet._sslverify import OpenSSLCertificateAuthorities

    class RecaptchaOpenSSLCertificateAuthorities(OpenSSLCertificateAuthorities):
        """The trusted CAs for connecting to reCAPTCHA servers."""
        #: A list of `OpenSSL.crypto.X509` objects.
        caCerts = [GOOGLE_INTERNET_AUTHORITY_CA_CERT,]
        def __init__(self):
            super(RecaptchaOpenSSLCertificateAuthorities, self).__init__(self.caCerts)
Beispiel #11
0
# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'Twisted'
copyright = u'2014, Twisted Matrix Labs'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = "{major}.{minor}".format(major=twisted_version_object.major,
                                   minor=twisted_version_object.minor)
# The full version, including alpha/beta/rc tags.
release = twisted_version_object.short()

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']
Beispiel #12
0
def _authenticate_with_kerberos(conn_info, url, agent, gss_client=None):
    service = '{0}@{1}'.format(conn_info.service.upper(), conn_info.hostname)
    if gss_client is None:
        gss_client = AuthGSSClient(service, conn_info)

    try:
        base64_client_data = yield gss_client.get_base64_client_data()
    except Exception as e:
        log.debug('{} error in get_base64_client_data: {}'.format(
            conn_info.hostname, e))
        raise e
    auth = 'Kerberos {0}'.format(base64_client_data)
    k_headers = Headers(_CONTENT_TYPE)
    k_headers.addRawHeader('Authorization', auth)
    # starting from twisted 17.1.0 for POST request with empty body twisted puts
    # content-length: 0 by default
    if LooseVersion(TWISTED_VERSION.short()) < LooseVersion("17.1.0"):
        k_headers.addRawHeader('Content-Length', '0')
    log.debug('%s sending auth data', conn_info.hostname)
    response = yield agent.request('POST', url, k_headers, None)
    log.debug('%s received authorization response code %d', conn_info.hostname,
              response.code)
    err_msg = None
    try:
        auth_header = response.headers.getRawHeaders('WWW-Authenticate')[0]
        auth_details = get_auth_details(auth_header)
    except Exception:
        auth_details = None
        err_msg = 'No WWW-Authenticate header found in authentication '\
                  'response.  Be sure Windows Management Framework is '\
                  'up to date.'
        log.debug(err_msg)

    if response.code == httplib.UNAUTHORIZED:
        try:
            if auth_details:
                yield gss_client._step(auth_details)
        except kerberos.GSSError as e:
            msg = "Unauthorized: Received kerberos error during "\
                "authentication of connection {0}: {1}.".format(
                    e.args[1][1], e.args[1][0])
            raise Exception(msg)
        raise UnauthorizedError(
            "Unauthorized to use winrm on {}. Must be Administrator or "
            "user given permissions to use winrm".format(conn_info.hostname))
    elif response.code == httplib.FORBIDDEN:
        raise ForbiddenError("Forbidden. Check WinRM port and version.")
    elif response.code != httplib.OK:
        proto = _StringProtocol()
        response.deliverBody(proto)
        xml_str = yield proto.d
        xml_str = gss_client.decrypt_body(xml_str)
        raise Exception(
            "status code {0} received on initial winrm connection {1}".format(
                response.code, xml_str or err_msg))
    if not auth_details:
        raise Exception(
            'negotiate not found in WWW-Authenticate header: {0}'.format(
                auth_header))
    k_username = yield gss_client.get_username(auth_details)
    log.debug('{} kerberos auth successful for user: {} / {} '.format(
        conn_info.hostname, conn_info.username, k_username))
    defer.returnValue(gss_client)
Beispiel #13
0
API_SERVER = API_SSL_SERVER
API_SSL_VERIFY_URL = "%s/verify" % API_SSL_SERVER

# `t.w.client.HTTPConnectionPool` isn't available in Twisted-12.0.0 (see
# ticket #11219):
_connectionPoolAvailable = _twistedversion >= Version('twisted', 12, 1, 0)
if _connectionPoolAvailable:
    logging.info("Using HTTPConnectionPool for reCaptcha API server.")
    _pool = client.HTTPConnectionPool(reactor, persistent=False)
    _pool.maxPersistentPerHost = 5
    _pool.cachedConnectionTimeout = 30
    _agent = client.Agent(reactor, pool=_pool)
else:
    logging.warn("Twisted-%s is too old for HTTPConnectionPool! Disabling..."
                 % _twistedversion.short())
    _pool = None
    _agent = client.Agent(reactor)


def _setAgent(agent):
    """Set the global :attr:`agent`.

    :param agent: An :api:`twisted.web.client.Agent` for issuing requests.
    """
    global _agent
    _agent = agent

def _getAgent(reactor=reactor, url=API_SSL_VERIFY_URL, connectTimeout=30,
              **kwargs):
    """Create a :api:`twisted.web.client.Agent` which will verify the
Beispiel #14
0
# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'Twisted'
copyright = u'2014, Twisted Matrix Labs'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = "{major}.{minor}".format(major=twisted_version_object.major,
                                   minor=twisted_version_object.minor)
# The full version, including alpha/beta/rc tags.
release = twisted_version_object.short()

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']