本文整理汇总了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))
示例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
示例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))
示例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
示例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
示例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
示例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)
)
示例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
示例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
示例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
示例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
示例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
示例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
示例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
示例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