/usr/lib/python2.7/dist-packages/cyclone/httpclient.py is in python-cyclone 1.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | # coding: utf-8
#
# Copyright 2010 Alexandre Fiori
# based on the original Tornado by Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Non-blocking HTTP client"""
import functools
import types
from cyclone import escape
from cyclone.web import HTTPError
from twisted.internet import defer
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
from twisted.web.iweb import IBodyProducer
from zope.interface import implements
agent = Agent(reactor)
class StringProducer(object):
implements(IBodyProducer)
def __init__(self, body):
self.body = body
self.length = len(body)
def startProducing(self, consumer):
consumer.write(self.body)
return defer.succeed(None)
def pauseProducing(self):
pass
def stopProducing(self):
pass
class Receiver(Protocol):
def __init__(self, finished):
self.finished = finished
self.data = []
def dataReceived(self, bytes):
self.data.append(bytes)
def connectionLost(self, reason):
self.finished.callback("".join(self.data))
class HTTPClient(object):
def __init__(self, url, *args, **kwargs):
self._args = args
self._kwargs = kwargs
self.url = url
self.followRedirect = self._kwargs.get("followRedirect", 0)
self.maxRedirects = self._kwargs.get("maxRedirects", 3)
self.headers = self._kwargs.get("headers", {})
self.body = self._kwargs.get("postdata")
self.method = self._kwargs.get("method", self.body and "POST" or "GET")
agent._connectTimeout = self._kwargs.get("timeout", None)
if self.method.upper() == "POST" and \
"Content-Type" not in self.headers:
self.headers["Content-Type"] = \
["application/x-www-form-urlencoded"]
self.response = None
if self.body:
self.body_producer = StringProducer(self.body)
else:
self.body_producer = None
@defer.inlineCallbacks
def fetch(self):
request_headers = Headers(self.headers)
response = yield agent.request(
self.method,
self.url,
request_headers,
self.body_producer)
mr = self.maxRedirects
while mr >= 1:
if response.code in (301, 302, 303) and self.followRedirect:
mr -= 1
headers = dict(response.headers.getAllRawHeaders())
location = headers.get("Location")
if location:
if isinstance(location, types.ListType):
location = location[0]
#print("redirecting to:", location)
response = yield agent.request(
"GET", # self.method,
location,
request_headers,
self.body_producer)
else:
break
else:
break
response.error = None
response.headers = dict(response.headers.getAllRawHeaders())
# HTTP 204 and 304 responses have no body
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
if response.code in (204, 304):
response.body = ''
else:
d = defer.Deferred()
response.deliverBody(Receiver(d))
response.body = yield d
response.request = self
defer.returnValue(response)
def fetch(url, *args, **kwargs):
"""A non-blocking HTTP client.
Example::
d = httpclient.fetch("http://google.com")
d.addCallback(on_response)
By default the client does not follow redirects on HTTP 301, 302, 303
or 307.
Parameters:
followRedirect: Boolean, to tell the client whether to follow redirects
or not. [default: False]
maxRedirects: Maximum number of redirects to follow. This is to avoid
infinite loops cause by misconfigured servers.
postdata: Data that accompanies the request. If a request ``method`` is not
set but ``postdata`` is, then it is automatically turned into
a ``POST`` and the ``Content-Type`` is set to
``application/x-www-form-urlencoded``.
headers: A python dictionary containing HTTP headers for this request.
Note that all values must be lists::
headers={"Content-Type": ["application/json"]}
The response is an object with the following attributes:
code: HTTP server response code.
phrase: Text that describe the response code. e.g.: 302 ``See Other``
headers: Response headers
length: Content length
body: The data, untouched
"""
return HTTPClient(escape.utf8(url), *args, **kwargs).fetch()
class JsonRPC:
"""JSON-RPC client.
Once instantiated, may be used to make multiple calls to the server.
Example::
cli = httpclient.JsonRPC("http://localhost:8888/jsonrpc")
response1 = yield cli.echo("foobar")
response2 = yield cli.sort(["foo", "bar"])
Note that in the example above, ``echo`` and ``sort`` are remote methods
provided by the server.
"""
def __init__(self, url):
self.__rpcId = 0
self.__rpcUrl = url
def __getattr__(self, attr):
return functools.partial(self.__rpcRequest, attr)
def __rpcRequest(self, method, *args):
q = escape.json_encode({"method": method, "params": args,
"id": self.__rpcId})
self.__rpcId += 1
r = defer.Deferred()
d = fetch(self.__rpcUrl, method="POST", postdata=q)
def _success(response, deferred):
if response.code == 200:
data = escape.json_decode(response.body)
error = data.get("error")
if error:
deferred.errback(Exception(error))
else:
deferred.callback(data.get("result"))
else:
deferred.errback(HTTPError(response.code, response.phrase))
def _failure(failure, deferred):
deferred.errback(failure)
d.addCallback(_success, r)
d.addErrback(_failure, r)
return r
|