Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
HTTPConnection Class Reference
Inheritance diagram for HTTPConnection:
HTTPSConnection

Public Member Functions

def __init__
 
def set_debuglevel
 
def connect
 
def close
 
def send
 
def putrequest
 
def putheader
 
def endheaders
 
def request
 
def getresponse
 

Data Fields

 sock
 
 host
 
 port
 
 debuglevel
 

Static Public Attributes

 response_class = HTTPResponse
 
 default_port = HTTP_PORT
 
int auto_open = 1
 
int debuglevel = 0
 

Detailed Description

Definition at line 329 of file httplib.py.

Constructor & Destructor Documentation

def __init__ (   self,
  host,
  port = None 
)

Definition at line 339 of file httplib.py.

340  def __init__(self, host, port=None):
341  self.sock = None
342  self.__response = None
343  self.__state = _CS_IDLE
344 
345  self._set_hostport(host, port)

Member Function Documentation

def close (   self)
Close the connection to the HTTP server.

Definition at line 381 of file httplib.py.

References HTTPConnection.__response, HTTPConnection.__state, FTP.sock, and HTTPConnection.sock.

382  def close(self):
383  """Close the connection to the HTTP server."""
384  if self.sock:
385  self.sock.close() # close it manually... there may be other refs
386  self.sock = None
387  if self.__response:
388  self.__response.close()
389  self.__response = None
390  self.__state = _CS_IDLE
def connect (   self)
Connect to the host and port specified in __init__.

Definition at line 360 of file httplib.py.

References HTTPResponse.debuglevel, HTTPConnection.debuglevel, FTP.host, HTTPConnection.host, FTP.port, HTTPConnection.port, FTP.sock, HTTPConnection.sock, and socket.socket().

361  def connect(self):
362  """Connect to the host and port specified in __init__."""
363  msg = "getaddrinfo returns an empty list"
364  for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
365  af, socktype, proto, canonname, sa = res
366  try:
367  self.sock = socket.socket(af, socktype, proto)
368  if self.debuglevel > 0:
369  print "connect: (%s, %s)" % (self.host, self.port)
370  self.sock.connect(sa)
371  except socket.error, msg:
372  if self.debuglevel > 0:
373  print 'connect fail:', (self.host, self.port)
374  if self.sock:
375  self.sock.close()
376  self.sock = None
377  continue
378  break
379  if not self.sock:
380  raise socket.error, msg
def endheaders (   self)
Indicate that the last header line has been sent to the server.

Definition at line 523 of file httplib.py.

References HTTPConnection.__state, HTTPConnection.send(), dispatcher_with_send.send(), file_wrapper.send(), and HTTP.send.

524  def endheaders(self):
525  """Indicate that the last header line has been sent to the server."""
526 
527  if self.__state == _CS_REQ_STARTED:
528  self.__state = _CS_REQ_SENT
529  else:
530  raise CannotSendHeader()
531 
532  self.send('\r\n')
def getresponse (   self)

Definition at line 564 of file httplib.py.

References HTTPConnection.__response, HTTPConnection.__state, Chunk.close(), openrsrc.close(), _Hqxcoderengine.close(), FileInput.close(), _Rlecoderengine.close(), HTTPResponse.close(), BinHex.close(), GzipFile.close(), _Hqxdecoderengine.close(), Aifc_read.close(), _Rledecoderengine.close(), HTTPConnection.close(), HexBin.close(), file_wrapper.close(), FTP.close(), HTTPResponse.debuglevel, HTTPConnection.debuglevel, HTTPConnection.response_class, FTP.sock, and HTTPConnection.sock.

565  def getresponse(self):
566  "Get the response from the server."
567 
568  # check if a prior response has been completed
569  if self.__response and self.__response.isclosed():
570  self.__response = None
571 
572  #
573  # if a prior response exists, then it must be completed (otherwise, we
574  # cannot read this response's header to determine the connection-close
575  # behavior)
576  #
577  # note: if a prior response existed, but was connection-close, then the
578  # socket and response were made independent of this HTTPConnection
579  # object since a new request requires that we open a whole new
580  # connection
581  #
582  # this means the prior response had one of two states:
583  # 1) will_close: this connection was reset and the prior socket and
584  # response operate independently
585  # 2) persistent: the response was retained and we await its
586  # isclosed() status to become true.
587  #
588  if self.__state != _CS_REQ_SENT or self.__response:
589  raise ResponseNotReady()
590 
591  if self.debuglevel > 0:
592  response = self.response_class(self.sock, self.debuglevel)
593  else:
594  response = self.response_class(self.sock)
595 
596  response.begin()
597  self.__state = _CS_IDLE
598 
599  if response.will_close:
600  # this effectively passes the connection to the response
601  self.close()
602  else:
603  # remember this, so we can tell when it is complete
604  self.__response = response
605 
606  return response
607 
def putheader (   self,
  header,
  value 
)
Send a request header line to the server.

For example: h.putheader('Accept', 'text/html')

Definition at line 512 of file httplib.py.

References HTTPConnection.__state, HTTPConnection.send(), dispatcher_with_send.send(), file_wrapper.send(), and HTTP.send.

513  def putheader(self, header, value):
514  """Send a request header line to the server.
515 
516  For example: h.putheader('Accept', 'text/html')
517  """
518  if self.__state != _CS_REQ_STARTED:
519  raise CannotSendHeader()
520 
521  str = '%s: %s\r\n' % (header, value)
522  self.send(str)
def putrequest (   self,
  method,
  url,
  skip_host = 0 
)
Send a request to the server.

`method' specifies an HTTP request method, e.g. 'GET'.
`url' specifies the object being requested, e.g. '/index.html'.

Definition at line 413 of file httplib.py.

References HTTPConnection.__response, HTTPConnection.__state, HTTPConnection._http_vsn, HTTPConnection._http_vsn_str, HTTPConnection.auto_open, FTP.host, HTTPConnection.host, FTP.port, HTTPConnection.port, HTTPConnection.putheader(), HTTPConnection.send(), dispatcher_with_send.send(), file_wrapper.send(), HTTP.send, and urlparse.urlsplit().

414  def putrequest(self, method, url, skip_host=0):
415  """Send a request to the server.
416 
417  `method' specifies an HTTP request method, e.g. 'GET'.
418  `url' specifies the object being requested, e.g. '/index.html'.
419  """
420 
421  # check if a prior response has been completed
422  if self.__response and self.__response.isclosed():
423  self.__response = None
424 
425  #
426  # in certain cases, we cannot issue another request on this connection.
427  # this occurs when:
428  # 1) we are in the process of sending a request. (_CS_REQ_STARTED)
429  # 2) a response to a previous request has signalled that it is going
430  # to close the connection upon completion.
431  # 3) the headers for the previous response have not been read, thus
432  # we cannot determine whether point (2) is true. (_CS_REQ_SENT)
433  #
434  # if there is no prior response, then we can request at will.
435  #
436  # if point (2) is true, then we will have passed the socket to the
437  # response (effectively meaning, "there is no prior response"), and
438  # will open a new one when a new request is made.
439  #
440  # Note: if a prior response exists, then we *can* start a new request.
441  # We are not allowed to begin fetching the response to this new
442  # request, however, until that prior response is complete.
443  #
444  if self.__state == _CS_IDLE:
445  self.__state = _CS_REQ_STARTED
446  else:
447  raise CannotSendRequest()
448 
449  if not url:
450  url = '/'
451  str = '%s %s %s\r\n' % (method, url, self._http_vsn_str)
452 
453  try:
454  self.send(str)
455  except socket.error, v:
456  # trap 'Broken pipe' if we're allowed to automatically reconnect
457  if v[0] != 32 or not self.auto_open:
458  raise
459  # try one more time (the socket was closed; this will reopen)
460  self.send(str)
461 
462  if self._http_vsn == 11:
463  # Issue some standard headers for better HTTP/1.1 compliance
464 
465  if not skip_host:
466  # this header is issued *only* for HTTP/1.1
467  # connections. more specifically, this means it is
468  # only issued when the client uses the new
469  # HTTPConnection() class. backwards-compat clients
470  # will be using HTTP/1.0 and those clients may be
471  # issuing this header themselves. we should NOT issue
472  # it twice; some web servers (such as Apache) barf
473  # when they see two Host: headers
474 
475  # If we need a non-standard port,include it in the
476  # header. If the request is going through a proxy,
477  # but the host of the actual URL, not the host of the
478  # proxy.
479 
480  netloc = ''
481  if url.startswith('http'):
482  nil, netloc, nil, nil, nil = urlsplit(url)
483 
484  if netloc:
485  self.putheader('Host', netloc)
486  elif self.port == HTTP_PORT:
487  self.putheader('Host', self.host)
488  else:
489  self.putheader('Host', "%s:%s" % (self.host, self.port))
490 
491  # note: we are assuming that clients will not attempt to set these
492  # headers since *this* library must deal with the
493  # consequences. this also means that when the supporting
494  # libraries are updated to recognize other forms, then this
495  # code should be changed (removed or updated).
496 
497  # we only want a Content-Encoding of "identity" since we don't
498  # support encodings such as x-gzip or x-deflate.
499  self.putheader('Accept-Encoding', 'identity')
500 
501  # we can accept "chunked" Transfer-Encodings, but no others
502  # NOTE: no TE header implies *only* "chunked"
503  #self.putheader('TE', 'chunked')
504 
505  # if TE is supplied in the header, then it must appear in a
506  # Connection header.
507  #self.putheader('Connection', 'TE')
508 
509  else:
510  # For HTTP/1.0, the server will assume "not chunked"
511  pass
def request (   self,
  method,
  url,
  body = None,
  headers = {} 
)
Send a complete request to the server.

Definition at line 533 of file httplib.py.

References HTTPConnection._send_request(), HTTPConnection.auto_open, HTTPConnection.endheaders(), HTTP.endheaders, HTTPConnection.putheader(), HTTPConnection.putrequest(), HTTP.putrequest, HTTPConnection.send(), dispatcher_with_send.send(), file_wrapper.send(), HTTP.send, and locale.str().

534  def request(self, method, url, body=None, headers={}):
535  """Send a complete request to the server."""
536 
537  try:
538  self._send_request(method, url, body, headers)
539  except socket.error, v:
540  # trap 'Broken pipe' if we're allowed to automatically reconnect
541  if v[0] != 32 or not self.auto_open:
542  raise
543  # try one more time
544  self._send_request(method, url, body, headers)
def send (   self,
  str 
)
Send `str' to the server.

Definition at line 391 of file httplib.py.

References HTTPConnection.auto_open, Chunk.close(), openrsrc.close(), _Hqxcoderengine.close(), FileInput.close(), _Rlecoderengine.close(), HTTPResponse.close(), BinHex.close(), GzipFile.close(), _Hqxdecoderengine.close(), Aifc_read.close(), _Rledecoderengine.close(), HTTPConnection.close(), HexBin.close(), file_wrapper.close(), FTP.close(), FTP.connect(), HTTPConnection.connect(), HTTPResponse.debuglevel, HTTPConnection.debuglevel, FTP.sock, and HTTPConnection.sock.

392  def send(self, str):
393  """Send `str' to the server."""
394  if self.sock is None:
395  if self.auto_open:
396  self.connect()
397  else:
398  raise NotConnected()
399 
400  # send the data to the server. if we get a broken pipe, then close
401  # the socket. we want to reconnect when somebody tries to send again.
402  #
403  # NOTE: we DO propagate the error, though, because we cannot simply
404  # ignore the error... the caller will know if they can retry.
405  if self.debuglevel > 0:
406  print "send:", repr(str)
407  try:
408  self.sock.sendall(str)
409  except socket.error, v:
410  if v[0] == 32: # Broken pipe
411  self.close()
412  raise
def set_debuglevel (   self,
  level 
)

Definition at line 357 of file httplib.py.

358  def set_debuglevel(self, level):
359  self.debuglevel = level

Field Documentation

int auto_open = 1
static

Definition at line 336 of file httplib.py.

int debuglevel = 0
static

Definition at line 337 of file httplib.py.

debuglevel

Definition at line 358 of file httplib.py.

default_port = HTTP_PORT
static

Definition at line 335 of file httplib.py.

host

Definition at line 354 of file httplib.py.

port

Definition at line 355 of file httplib.py.

response_class = HTTPResponse
static

Definition at line 334 of file httplib.py.

sock

Definition at line 340 of file httplib.py.


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