當前位置: 首頁>>代碼示例>>Python>>正文


Python client.FileBodyProducer方法代碼示例

本文整理匯總了Python中twisted.web.client.FileBodyProducer方法的典型用法代碼示例。如果您正苦於以下問題:Python client.FileBodyProducer方法的具體用法?Python client.FileBodyProducer怎麽用?Python client.FileBodyProducer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在twisted.web.client的用法示例。


在下文中一共展示了client.FileBodyProducer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: request

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def request(self, method, url, content=None, state=http.OK, add_headers=None):
        body = None if content is None else client.FileBodyProducer(
            StringIO(content))
        headers = {'Content-Type': ['application/json'], 'X-WebAuth-User': ['tester']}
        if add_headers:
            headers.update(add_headers)
        response = yield self.client.request(method,
                                             self.url_prefix + url,
                                             Headers(headers),
                                             body)
        self.assertEqual(state, response.code)
        body_receiver = BodyReceiver()
        response.deliverBody(body_receiver)
        body = yield body_receiver.finished
        if response.headers.getRawHeaders('content-type') == ['application/json']:
            body = anyjson.loads(body)
        returnValue((response, body)) 
開發者ID:moira-alert,項目名稱:worker,代碼行數:19,代碼來源:test_api.py

示例2: testReadInput

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def testReadInput(self):
        cgiFilename = os.path.abspath(self.mktemp())
        with open(cgiFilename, 'wt') as cgiFile:
            cgiFile.write(READINPUT_CGI)

        portnum = self.startServer(cgiFilename)
        agent = client.Agent(reactor)
        d = agent.request(
            uri="http://localhost:%d/cgi" % (portnum,),
            method=b"POST",
            bodyProducer=client.FileBodyProducer(
                BytesIO(b"Here is your stdin")),
        )
        d.addCallback(client.readBody)
        d.addCallback(self._testReadInput_1)
        return d 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:18,代碼來源:test_cgi.py

示例3: _agent

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def _agent(method, url, contextFactory=None, headers=None, body=None):
    kwargs = {}
    if contextFactory:
        kwargs['contextFactory'] = contextFactory
    agent = Agent(reactor, **kwargs)
    rbody = None
    if body:
        rbody = FileBodyProducer(StringIO(body))
    response = yield agent.request(method, url,
                                   headers=headers,
                                   bodyProducer=rbody)

    proto = AccumulatingProtocol()
    proto.closedDeferred = Deferred()
    response.deliverBody(proto)
    yield proto.closedDeferred

    returnValue((response, proto.data)) 
開發者ID:mozilla-services,項目名稱:autopush,代碼行數:20,代碼來源:test_integration.py

示例4: _send_notification

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def _send_notification(self, uaid, node_id, notification):
        """Send a notification to a specific node_id

        This version of the overriden method includes the necessary crypto
        headers for the notification.

        :type notification: autopush.utils.WebPushNotification

        """
        payload = notification.serialize()
        payload["timestamp"] = int(time.time())
        url = node_id + "/push/" + uaid
        request = self.agent.request(
            "PUT",
            url.encode("utf8"),
            bodyProducer=FileBodyProducer(StringIO(json.dumps(payload))),
        )
        request.addCallback(IgnoreBody.ignore)
        return request 
開發者ID:mozilla-services,項目名稱:autopush,代碼行數:21,代碼來源:webpush.py

示例5: test_ReadInput

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def test_ReadInput(self):
        cgiFilename = os.path.abspath(self.mktemp())
        with open(cgiFilename, 'wt') as cgiFile:
            cgiFile.write(READINPUT_CGI)

        portnum = self.startServer(cgiFilename)
        agent = client.Agent(reactor)
        url = "http://localhost:%d/cgi" % (portnum,)
        url = url.encode("ascii")
        d = agent.request(
            uri=url,
            method=b"POST",
            bodyProducer=client.FileBodyProducer(
                BytesIO(b"Here is your stdin")),
        )
        d.addCallback(client.readBody)
        d.addCallback(self._test_ReadInput_1)
        return d 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:20,代碼來源:test_cgi.py

示例6: test_ReadAllInput

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def test_ReadAllInput(self):
        cgiFilename = os.path.abspath(self.mktemp())
        with open(cgiFilename, 'wt') as cgiFile:
            cgiFile.write(READALLINPUT_CGI)

        portnum = self.startServer(cgiFilename)
        url = "http://localhost:%d/cgi" % (portnum,)
        url = url.encode("ascii")
        d = client.Agent(reactor).request(
            uri=url,
            method=b"POST",
            bodyProducer=client.FileBodyProducer(
                BytesIO(b"Here is your stdin")),
        )
        d.addCallback(client.readBody)
        d.addCallback(self._test_ReadAllInput_1)
        return d 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:19,代碼來源:test_cgi.py

示例7: test_power_off

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def test_power_off(self):
        driver = OpenBMCPowerDriver()
        system_id = factory.make_name("system_id")
        context = make_context()
        url = driver.get_uri(context, HOST_STATE + "RequestedHostTransition")
        mock_file_body_producer = self.patch(
            openbmc_module, "FileBodyProducer"
        )
        dataoff = FileBodyProducer(
            BytesIO(json.dumps(HOST_OFF).encode("utf-8"))
        )
        mock_openbmc_request = self.patch(driver, "openbmc_request")
        mock_openbmc_request.return_value = dataoff
        mock_file_body_producer.return_value = dataoff
        mock_command = self.patch(driver, "command")
        mock_command.return_value = SAMPLE_JSON_HOSTOFF
        mock_set_pxe_boot = self.patch(driver, "set_pxe_boot")

        yield driver.power_off(system_id, context)
        self.assertThat(mock_set_pxe_boot, MockCalledOnceWith(context))
        self.assertThat(
            mock_command, MockCalledOnceWith(context, b"PUT", url, dataoff)
        ) 
開發者ID:maas,項目名稱:maas,代碼行數:25,代碼來源:test_openbmc.py

示例8: test_hpe_create_volume_invalid_provisioning_option

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def test_hpe_create_volume_invalid_provisioning_option(self):
        name = 'test-create-volume-fake'
        path = b"/VolumeDriver.Create"
        body = {u"Name": name,
                u"Opts": {u"provisioning": u"fake"}}

        headers = Headers({b"content-type": [b"application/json"]})
        body_producer = FileBodyProducer(BytesIO(dumps(body)))
        agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory())
        d = agent.request(b'POST', b"UNIX://localhost" + path, headers,
                          body_producer)
        d.addCallback(self.checkResponse, json.dumps({
            u"Err": "Invalid input received: Must specify a valid " +
            "provisioning type ['thin', 'full', " +
            "'dedup'], value 'fake' is invalid."}))
        d.addCallback(self._remove_volume_callback, name)
        d.addErrback(self.cbFailed)
        return d 
開發者ID:hpe-storage,項目名稱:python-hpedockerplugin,代碼行數:20,代碼來源:test_hpe_plugin.py

示例9: test_hpe_create_volume_invalid_option

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def test_hpe_create_volume_invalid_option(self):
        name = 'test-create-volume-fake'
        path = b"/VolumeDriver.Create"
        body = {u"Name": name,
                u"Opts": {u"fake": u"fake"}}

        headers = Headers({b"content-type": [b"application/json"]})
        body_producer = FileBodyProducer(BytesIO(dumps(body)))
        agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory())
        d = agent.request(b'POST', b"UNIX://localhost" + path, headers,
                          body_producer)
        d.addCallback(self.checkResponse, json.dumps({
            u"Err": "create volume failed, error is: fake is not a valid "
            "option. Valid options are: ['size', 'provisioning', "
            "'flash-cache']"}))
        d.addCallback(self._remove_volume_callback, name)
        d.addErrback(self.cbFailed)
        return d 
開發者ID:hpe-storage,項目名稱:python-hpedockerplugin,代碼行數:20,代碼來源:test_hpe_plugin.py

示例10: _get_volume_mount_path

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def _get_volume_mount_path(self, body, name):
        # NOTE: body arg is the result from last deferred call.
        # Python complains about parameter mis-match if you don't include it
        # In this test, we need it to compare expected results with Path
        # request

        # Compare path returned by mount (body) with Get Path request
        path = b"/VolumeDriver.Path"
        newbody = {u"Name": name}
        headers = Headers({b"content-type": [b"application/json"]})
        body_producer = FileBodyProducer(BytesIO(dumps(newbody)))
        agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory())
        d = agent.request(b'POST', b"UNIX://localhost" + path, headers,
                          body_producer)
        d.addCallback(self.checkResponse, body)
        d.addErrback(self.cbFailed)
        return d 
開發者ID:hpe-storage,項目名稱:python-hpedockerplugin,代碼行數:19,代碼來源:test_hpe_plugin.py

示例11: _mount_the_volume

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def _mount_the_volume(self, body, name):
        # NOTE: body arg is the result from last deferred call.
        # Python complains about parameter mis-match if you don't include it

        # Mount the previously created volume
        path = b"/VolumeDriver.Mount"
        newbody = {u"Name": name}
        headers = Headers({b"content-type": [b"application/json"]})
        body_producer = FileBodyProducer(BytesIO(dumps(newbody)))
        agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory())
        d = agent.request(b'POST', b"UNIX://localhost" + path, headers,
                          body_producer)

        d.addCallback(self.getResponse)

        # If we get a valid response from Path request then we assume
        # the mount passed.
        # TODO: Add additonal logic to verify the mountpath
        d.addCallback(self._get_volume_mount_path, name)
        return d 
開發者ID:hpe-storage,項目名稱:python-hpedockerplugin,代碼行數:22,代碼來源:test_hpe_plugin.py

示例12: broken_test_hpe_mount_umount_volume

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def broken_test_hpe_mount_umount_volume(self):
        name = 'test-mount-volume'
        path = b"/VolumeDriver.Create"
        body = {u"Name": name}

        # Create a volume to be mounted
        headers = Headers({b"content-type": [b"application/json"]})
        body_producer = FileBodyProducer(BytesIO(dumps(body)))
        agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory())
        d = agent.request(b'POST', b"UNIX://localhost" + path, headers,
                          body_producer)
        d.addCallback(self.checkResponse, json.dumps({u"Err": ''}))
        d.addErrback(self.cbFailed)

        # Mount the previously created volume
        d.addCallback(self._mount_the_volume, name)

        # UMount the previously created volume
        d.addCallback(self._unmount_the_volume, name)

        # Remove the previously created volume
        d.addCallback(self._remove_volume_callback, name)
        return d 
開發者ID:hpe-storage,項目名稱:python-hpedockerplugin,代碼行數:25,代碼來源:test_hpe_plugin.py

示例13: test_hpe_get_volume

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def test_hpe_get_volume(self):
        name = 'test-get-volume'
        path = b"/VolumeDriver.Create"
        body = {u"Name": name}

        # Create a volume to be mounted
        headers = Headers({b"content-type": [b"application/json"]})
        body_producer = FileBodyProducer(BytesIO(dumps(body)))
        agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory())
        d = agent.request(b'POST', b"UNIX://localhost" + path, headers,
                          body_producer)
        d.addCallback(self.checkResponse, json.dumps({u"Err": ''}))
        d.addErrback(self.cbFailed)

        # Get the previously created volume
        expected = {u"Volume": {u"Status": {},
                                u"Mountpoint": '',
                                u"Name": name},
                    u"Err": ''}
        d.addCallback(self._get_volume, name, expected)

        # Remove the previously created volume
        d.addCallback(self._remove_volume_callback, name)
        return d 
開發者ID:hpe-storage,項目名稱:python-hpedockerplugin,代碼行數:26,代碼來源:test_hpe_plugin.py

示例14: broken_test_hpe_list_volume

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def broken_test_hpe_list_volume(self):
        name = 'test-list-volume'
        path = b"/VolumeDriver.Create"
        body = {u"Name": name}

        # Create a volume to be mounted
        headers = Headers({b"content-type": [b"application/json"]})
        body_producer = FileBodyProducer(BytesIO(dumps(body)))
        agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory())
        d = agent.request(b'POST', b"UNIX://localhost" + path, headers,
                          body_producer)
        d.addCallback(self.checkResponse, json.dumps({u"Err": ''}))
        d.addErrback(self.cbFailed)

        # List volumes
        expected = {u"Err": '',
                    u"Volumes": [{u"Mountpoint": '',
                                  u"Name": name}]}
        d.addCallback(self._list_volumes, name, expected)

        # Remove the previously created volume
        d.addCallback(self._remove_volume_callback, name)

        return d 
開發者ID:hpe-storage,項目名稱:python-hpedockerplugin,代碼行數:26,代碼來源:test_hpe_plugin.py

示例15: request

# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import FileBodyProducer [as 別名]
def request(self, method, url, body=None):
        def makeRequest(ignored):
            bodyProducer = None
            if body is not None:
                bodyProducer = FileBodyProducer(six.StringIO(body))

            headers = {}
            for key, value in six.iteritems(self.headers):
                headers[key] = [value]

            agent = Agent(reactor, pool=TwistedRequestDriver.pool)
            d = agent.request(method, url, Headers(headers), bodyProducer)
            d.addCallback(self.receive)
            return d

        def releaseSemaphore(result):
            TwistedRequestDriver.sem.release()

            # Forward the result to the next handler.
            return result

        d = TwistedRequestDriver.sem.acquire()

        # Make the request once we acquire the semaphore.
        d.addCallback(makeRequest)

        # Release the semaphore regardless of how the request goes.
        d.addBoth(releaseSemaphore)
        return d 
開發者ID:ParadropLabs,項目名稱:Paradrop,代碼行數:31,代碼來源:http.py


注:本文中的twisted.web.client.FileBodyProducer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。