Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
HTTPResponse Class Reference

Public Member Functions

def __init__
 
def begin
 
def close
 
def isclosed
 
def read
 
def getheader
 

Data Fields

 fp
 
 debuglevel
 
 msg
 
 version
 
 status
 
 reason
 
 chunked
 
 chunk_left
 
 length
 note: we shouldn't have any trailers! More...
 
 will_close
 

Detailed Description

Definition at line 97 of file httplib.py.

Constructor & Destructor Documentation

def __init__ (   self,
  sock,
  debuglevel = 0 
)

Definition at line 98 of file httplib.py.

98 
99  def __init__(self, sock, debuglevel=0):
100  self.fp = sock.makefile('rb', 0)
101  self.debuglevel = debuglevel
103  self.msg = None
104 
105  # from the Status-Line of the response
106  self.version = _UNKNOWN # HTTP-Version
107  self.status = _UNKNOWN # Status-Code
108  self.reason = _UNKNOWN # Reason-Phrase
110  self.chunked = _UNKNOWN # is "chunked" being used?
111  self.chunk_left = _UNKNOWN # bytes left to read in current chunk
112  self.length = _UNKNOWN # number of bytes left in response
113  self.will_close = _UNKNOWN # conn will close at end of response

Member Function Documentation

def begin (   self)

Definition at line 114 of file httplib.py.

References HTTPResponse.chunk_left, HTTPResponse.chunked, Chunk.close(), openrsrc.close(), _Hqxcoderengine.close(), FileInput.close(), _Rlecoderengine.close(), HTTPResponse.close(), BinHex.close(), GzipFile.close(), _Hqxdecoderengine.close(), Aifc_read.close(), _Rledecoderengine.close(), HexBin.close(), file_wrapper.close(), FTP.close(), HTTPResponse.debuglevel, HTTPResponse.fp, FieldStorage.fp, HTTPResponse.length, FieldStorage.length, GetoptError.msg, HTTPResponse.msg, HTTPResponse.reason, HTTPResponse.status, HTTPResponse.version, and HTTPResponse.will_close.

115  def begin(self):
116  if self.msg is not None:
117  # we've already started reading the response
118  return
119 
120  line = self.fp.readline()
121  if self.debuglevel > 0:
122  print "reply:", repr(line)
123  try:
124  [version, status, reason] = line.split(None, 2)
125  except ValueError:
126  try:
127  [version, status] = line.split(None, 1)
128  reason = ""
129  except ValueError:
130  version = "HTTP/0.9"
131  status = "200"
132  reason = ""
133  if version[:5] != 'HTTP/':
134  self.close()
135  raise BadStatusLine(line)
136 
137  # The status code is a three-digit number
138  try:
139  self.status = status = int(status)
140  if status < 100 or status > 999:
141  raise BadStatusLine(line)
142  except ValueError:
143  raise BadStatusLine(line)
144  self.reason = reason.strip()
145 
146  if version == 'HTTP/1.0':
147  self.version = 10
148  elif version.startswith('HTTP/1.'):
149  self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1
150  elif version == 'HTTP/0.9':
151  self.version = 9
152  else:
153  raise UnknownProtocol(version)
154 
155  if self.version == 9:
156  self.msg = mimetools.Message(StringIO())
157  return
158 
159  self.msg = mimetools.Message(self.fp, 0)
160  if self.debuglevel > 0:
161  for hdr in self.msg.headers:
162  print "header:", hdr,
163 
164  # don't let the msg keep an fp
165  self.msg.fp = None
166 
167  # are we using the chunked-style of transfer encoding?
168  tr_enc = self.msg.getheader('transfer-encoding')
169  if tr_enc:
170  if tr_enc.lower() != 'chunked':
172  self.chunked = 1
173  self.chunk_left = None
174  else:
175  self.chunked = 0
176 
177  # will the connection close at the end of the response?
178  conn = self.msg.getheader('connection')
179  if conn:
180  conn = conn.lower()
181  # a "Connection: close" will always close the connection. if we
182  # don't see that and this is not HTTP/1.1, then the connection will
183  # close unless we see a Keep-Alive header.
184  self.will_close = conn.find('close') != -1 or \
185  ( self.version != 11 and \
186  not self.msg.getheader('keep-alive') )
187  else:
188  # for HTTP/1.1, the connection will always remain open
189  # otherwise, it will remain open IFF we see a Keep-Alive header
190  self.will_close = self.version != 11 and \
191  not self.msg.getheader('keep-alive')
192 
193  # do we have a Content-Length?
194  # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
195  length = self.msg.getheader('content-length')
196  if length and not self.chunked:
197  try:
198  self.length = int(length)
199  except ValueError:
200  self.length = None
201  else:
202  self.length = None
203 
204  # does the body have a fixed length? (of zero)
205  if (status == 204 or # No Content
206  status == 304 or # Not Modified
207  100 <= status < 200): # 1xx codes
208  self.length = 0
209 
210  # if the connection remains open, and we aren't using chunked, and
211  # a content-length was not provided, then assume that the connection
212  # WILL close.
213  if not self.will_close and \
214  not self.chunked and \
215  self.length is None:
216  self.will_close = 1
def close (   self)

Definition at line 217 of file httplib.py.

References HTTPResponse.fp, and FieldStorage.fp.

218  def close(self):
219  if self.fp:
220  self.fp.close()
221  self.fp = None
def getheader (   self,
  name,
  default = None 
)

Definition at line 323 of file httplib.py.

References GetoptError.msg, and HTTPResponse.msg.

324  def getheader(self, name, default=None):
325  if self.msg is None:
326  raise ResponseNotReady()
327  return self.msg.getheader(name, default)
328 
def isclosed (   self)

Definition at line 222 of file httplib.py.

References HTTPResponse.fp, and FieldStorage.fp.

223  def isclosed(self):
224  # NOTE: it is possible that we will not ever call self.close(). This
225  # case occurs when will_close is TRUE, length is None, and we
226  # read up to the last byte, but NOT past it.
227  #
228  # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be
229  # called, meaning self.isclosed() is meaningful.
230  return self.fp is None
def read (   self,
  amt = None 
)

Definition at line 231 of file httplib.py.

References HTTPResponse._safe_read(), HTTPResponse.chunk_left, HTTPResponse.chunked, Chunk.close(), openrsrc.close(), _Hqxcoderengine.close(), FileInput.close(), _Rlecoderengine.close(), HTTPResponse.close(), BinHex.close(), GzipFile.close(), _Hqxdecoderengine.close(), Aifc_read.close(), _Rledecoderengine.close(), HexBin.close(), file_wrapper.close(), FTP.close(), HTTPResponse.fp, FieldStorage.fp, HTTPResponse.length, FieldStorage.length, and HTTPResponse.will_close.

232  def read(self, amt=None):
233  if self.fp is None:
234  return ''
235 
236  if self.chunked:
237  chunk_left = self.chunk_left
238  value = ''
239  while 1:
240  if chunk_left is None:
241  line = self.fp.readline()
242  i = line.find(';')
243  if i >= 0:
244  line = line[:i] # strip chunk-extensions
245  chunk_left = int(line, 16)
246  if chunk_left == 0:
247  break
248  if amt is None:
249  value = value + self._safe_read(chunk_left)
250  elif amt < chunk_left:
251  value = value + self._safe_read(amt)
252  self.chunk_left = chunk_left - amt
253  return value
254  elif amt == chunk_left:
255  value = value + self._safe_read(amt)
256  self._safe_read(2) # toss the CRLF at the end of the chunk
257  self.chunk_left = None
258  return value
259  else:
260  value = value + self._safe_read(chunk_left)
261  amt = amt - chunk_left
262 
263  # we read the whole chunk, get another
264  self._safe_read(2) # toss the CRLF at the end of the chunk
265  chunk_left = None
266 
267  # read and discard trailer up to the CRLF terminator
268  ### note: we shouldn't have any trailers!
269  while 1:
270  line = self.fp.readline()
271  if line == '\r\n':
272  break
273 
274  # we read everything; close the "file"
275  self.close()
276 
277  return value
278 
279  elif amt is None:
280  # unbounded read
281  if self.will_close:
282  s = self.fp.read()
283  else:
284  s = self._safe_read(self.length)
285  self.close() # we read everything
286  return s
287 
288  if self.length is not None:
289  if amt > self.length:
290  # clip the read to the "end of response"
291  amt = self.length
292  self.length = self.length - amt
293 
294  # we do not use _safe_read() here because this may be a .will_close
295  # connection, and the user is reading more bytes than will be provided
296  # (for example, reading in 1k chunks)
297  s = self.fp.read(amt)
298 
299  return s

Field Documentation

chunk_left

Definition at line 110 of file httplib.py.

chunked

Definition at line 109 of file httplib.py.

debuglevel

Definition at line 100 of file httplib.py.

fp

Definition at line 99 of file httplib.py.

length

note: we shouldn't have any trailers!

Definition at line 111 of file httplib.py.

msg

Definition at line 102 of file httplib.py.

reason

Definition at line 107 of file httplib.py.

status

Definition at line 106 of file httplib.py.

version

Definition at line 105 of file httplib.py.

will_close

Definition at line 112 of file httplib.py.


The documentation for this class was generated from the following file: