本文整理汇总了Python中twisted.web.client.HTTPClientFactory方法的典型用法代码示例。如果您正苦于以下问题:Python client.HTTPClientFactory方法的具体用法?Python client.HTTPClientFactory怎么用?Python client.HTTPClientFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.client
的用法示例。
在下文中一共展示了client.HTTPClientFactory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_infiniteRedirection
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_infiniteRedirection(self):
"""
When more than C{redirectLimit} HTTP redirects are encountered, the
page request fails with L{InfiniteRedirection}.
"""
def checkRedirectCount(*a):
self.assertEqual(f._redirectCount, 13)
self.assertEqual(self.infiniteRedirectResource.count, 13)
f = client._makeGetterFactory(
self.getURL('infiniteRedirect'),
client.HTTPClientFactory,
redirectLimit=13)
d = self.assertFailure(f.deferred, error.InfiniteRedirection)
d.addCallback(checkRedirectCount)
return d
示例2: test_afterFoundGet
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_afterFoundGet(self):
"""
Enabling unsafe redirection behaviour overwrites the method of
redirected C{POST} requests with C{GET}.
"""
url = self.getURL('extendedRedirect?code=302')
f = client.HTTPClientFactory(url, followRedirect=True, method=b"POST")
self.assertFalse(
f.afterFoundGet,
"By default, afterFoundGet must be disabled")
def gotPage(page):
self.assertEqual(
self.extendedRedirect.lastMethod,
b"GET",
"With afterFoundGet, the HTTP method must change to GET")
d = client.getPage(
url, followRedirect=True, afterFoundGet=True, method=b"POST")
d.addCallback(gotPage)
return d
示例3: _getPage
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def _getPage(url, descriptor):
"""
Fetch the body of the given url via HTTP, connecting to the given host
and port.
@param url: The URL to GET
@type url: C{str}
@param descriptor: The endpoint descriptor to use
@type descriptor: C{str}
@return: A deferred; upon 200 success the body of the response is returned,
otherwise a twisted.web.error.Error is the result.
"""
point = endpoints.clientFromString(reactor, descriptor)
factory = HTTPClientFactory(url, timeout=10)
point.connect(factory)
return factory.deferred
示例4: makeRequest
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def makeRequest(self, path, method, headers, body):
scheme = "https:" if self.useSSL else "http:"
url = "%s//%s:%d%s" % (scheme, self.host, self.port, path)
caldavFactory = client.HTTPClientFactory(
url, method=method,
headers=headers, postdata=body, agent="Push Monitor")
caldavFactory.username = self.authname
caldavFactory.password = self.password
caldavFactory.noisy = False
caldavFactory.protocol = PropfindRequestor
if self.useSSL:
connect(GAIEndpoint(reactor, self.host, self.port, simpleClientContextFactory(self.host)),
caldavFactory)
else:
connect(GAIEndpoint(reactor, self.host, self.port), caldavFactory)
return caldavFactory.deferred
示例5: test_infiniteRedirection
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_infiniteRedirection(self):
"""
When more than C{redirectLimit} HTTP redirects are encountered, the
page request fails with L{InfiniteRedirection}.
"""
def checkRedirectCount(*a):
self.assertEquals(f._redirectCount, 13)
self.assertEquals(self.infiniteRedirectResource.count, 13)
f = client._makeGetterFactory(
self.getURL('infiniteRedirect'),
client.HTTPClientFactory,
redirectLimit=13)
d = self.assertFailure(f.deferred, error.InfiniteRedirection)
d.addCallback(checkRedirectCount)
return d
示例6: test_afterFoundGet
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_afterFoundGet(self):
"""
Enabling unsafe redirection behaviour overwrites the method of
redirected C{POST} requests with C{GET}.
"""
url = self.getURL('extendedRedirect?code=302')
f = client.HTTPClientFactory(url, followRedirect=True, method="POST")
self.assertFalse(
f.afterFoundGet,
"By default, afterFoundGet must be disabled")
def gotPage(page):
self.assertEquals(
self.extendedRedirect.lastMethod,
"GET",
"With afterFoundGet, the HTTP method must change to GET")
d = client.getPage(
url, followRedirect=True, afterFoundGet=True, method="POST")
d.addCallback(gotPage)
return d
示例7: test_protectedServerAndDate
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_protectedServerAndDate(self):
"""
If the CGI script emits a I{Server} or I{Date} header, these are
ignored.
"""
cgiFilename = self.writeCGI(SPECIAL_HEADER_CGI)
portnum = self.startServer(cgiFilename)
url = "http://localhost:%d/cgi" % (portnum,)
factory = client.HTTPClientFactory(url)
reactor.connectTCP('localhost', portnum, factory)
def checkResponse(ignored):
self.assertNotIn('monkeys', factory.response_headers['server'])
self.assertNotIn('last year', factory.response_headers['date'])
factory.deferred.addCallback(checkResponse)
return factory.deferred
示例8: test_duplicateHeaderCGI
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_duplicateHeaderCGI(self):
"""
If a CGI script emits two instances of the same header, both are sent in
the response.
"""
cgiFilename = self.writeCGI(DUAL_HEADER_CGI)
portnum = self.startServer(cgiFilename)
url = "http://localhost:%d/cgi" % (portnum,)
factory = client.HTTPClientFactory(url)
reactor.connectTCP('localhost', portnum, factory)
def checkResponse(ignored):
self.assertEquals(
factory.response_headers['header'], ['spam', 'eggs'])
factory.deferred.addCallback(checkResponse)
return factory.deferred
示例9: test_earlyHeaders
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_earlyHeaders(self):
"""
When a connection is made, L{HTTPPagerGetter} sends the headers from
its factory's C{headers} dict. If I{Host} or I{Content-Length} is
present in this dict, the values are not sent, since they are sent with
special values before the C{headers} dict is processed. If
I{User-Agent} is present in the dict, it overrides the value of the
C{agent} attribute of the factory. If I{Cookie} is present in the
dict, its value is added to the values from the factory's C{cookies}
attribute.
"""
factory = client.HTTPClientFactory(
b'http://foo/bar',
agent=b"foobar",
cookies={b'baz': b'quux'},
postdata=b"some data",
headers={
b'Host': b'example.net',
b'User-Agent': b'fooble',
b'Cookie': b'blah blah',
b'Content-Length': b'12981',
b'Useful': b'value'})
transport = StringTransport()
protocol = client.HTTPPageGetter()
protocol.factory = factory
protocol.makeConnection(transport)
result = transport.value()
for expectedHeader in [
b"Host: example.net\r\n",
b"User-Agent: foobar\r\n",
b"Content-Length: 9\r\n",
b"Useful: value\r\n",
b"connection: close\r\n",
b"Cookie: blah blah; baz=quux\r\n"]:
self.assertIn(expectedHeader, result)
示例10: testFactoryInfo
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def testFactoryInfo(self):
url = self.getURL('file')
uri = client.URI.fromBytes(url)
factory = client.HTTPClientFactory(url)
reactor.connectTCP(nativeString(uri.host), uri.port, factory)
return factory.deferred.addCallback(self._cbFactoryInfo, factory)
示例11: test_setURL
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_setURL(self):
"""
L{client.HTTPClientFactory.setURL} alters the scheme, host, port and
path for absolute URLs.
"""
url = b'http://example.com'
f = client.HTTPClientFactory(url)
self.assertEqual(
(url, b'http', b'example.com', 80, b'/'),
(f.url, f.scheme, f.host, f.port, f.path))
示例12: test_setURLRelativePath
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_setURLRelativePath(self):
"""
L{client.HTTPClientFactory.setURL} alters the path in a relative URL.
"""
f = client.HTTPClientFactory(b'http://example.com')
url = b'/hello'
f.setURL(url)
self.assertEqual(
(url, b'http', b'example.com', 80, b'/hello'),
(f.url, f.scheme, f.host, f.port, f.path))
示例13: testCookieHeaderParsing
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def testCookieHeaderParsing(self):
factory = client.HTTPClientFactory(b'http://foo.example.com/')
proto = factory.buildProtocol('127.42.42.42')
transport = StringTransport()
proto.makeConnection(transport)
for line in [
b'200 Ok',
b'Squash: yes',
b'Hands: stolen',
b'Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT',
b'Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/',
b'Set-Cookie: SHIPPING=FEDEX; path=/foo',
b'',
b'body',
b'more body',
]:
proto.dataReceived(line + b'\r\n')
self.assertEqual(transport.value(),
b'GET / HTTP/1.0\r\n'
b'Host: foo.example.com\r\n'
b'User-Agent: Twisted PageGetter\r\n'
b'\r\n')
self.assertEqual(factory.cookies,
{
b'CUSTOMER': b'WILE_E_COYOTE',
b'PART_NUMBER': b'ROCKET_LAUNCHER_0001',
b'SHIPPING': b'FEDEX',
})
示例14: test_HTTPDefaultPort
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_HTTPDefaultPort(self):
"""
No port should be included in the host header when connecting to the
default HTTP port.
"""
factory = client.HTTPClientFactory(b'http://foo.example.com/')
proto = factory.buildProtocol(b'127.42.42.42')
proto.makeConnection(StringTransport())
self.assertEqual(self._getHost(proto.transport.value()),
b'foo.example.com')
示例15: test_HTTPPort80
# 需要导入模块: from twisted.web import client [as 别名]
# 或者: from twisted.web.client import HTTPClientFactory [as 别名]
def test_HTTPPort80(self):
"""
No port should be included in the host header when connecting to the
default HTTP port even if it is in the URL.
"""
factory = client.HTTPClientFactory(b'http://foo.example.com:80/')
proto = factory.buildProtocol('127.42.42.42')
proto.makeConnection(StringTransport())
self.assertEqual(self._getHost(proto.transport.value()),
b'foo.example.com')