Package Peach :: Package Publishers :: Module http
[hide private]

Source Code for Module Peach.Publishers.http

  1   
  2  ''' 
  3  Send something by HTTP. 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id: http.py 817 2008-03-26 22:31:40Z meddingt $ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2008 Michael Eddington 
 11  # 
 12  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 13  # of this software and associated documentation files (the "Software"), to deal 
 14  # in the Software without restriction, including without limitation the rights  
 15  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 16  # copies of the Software, and to permit persons to whom the Software is  
 17  # furnished to do so, subject to the following conditions: 
 18  # 
 19  # The above copyright notice and this permission notice shall be included in     
 20  # all copies or substantial portions of the Software. 
 21  # 
 22  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 23  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 24  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 25  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 26  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 27  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 28  # SOFTWARE. 
 29  # 
 30   
 31  # Authors: 
 32  #   Michael Eddington (mike@phed.org) 
 33   
 34  # $Id: http.py 817 2008-03-26 22:31:40Z meddingt $ 
 35   
 36  import time, urllib2 
 37  from Peach.publisher import Publisher 
 38   
39 -class HttpDigestAuth(Publisher):
40 ''' 41 A simple HTTP publisher. 42 ''' 43
44 - def __init__(self, url, realm, username, password, headers = None, timeout = 0.1):
45 ''' 46 @type host: string 47 @param host: Remote host 48 @type port: number 49 @param port: Remote port 50 @type timeout: number 51 @param timeout: How long to wait for reponse 52 ''' 53 self._url = url 54 self._realm = realm 55 self._username = username 56 self._password = password 57 self._headers = headers 58 self._timeout = float(timeout) 59 self._fd = None
60
61 - def stop(self):
62 ''' 63 Close connection if open. 64 ''' 65 self.close()
66
67 - def connect(self):
68 pass
69
70 - def close(self):
71 if self._fd: 72 self._fd.close() 73 self._fd = None
74
75 - def send(self, data):
76 ''' 77 Send data via sendall. 78 79 @type data: string 80 @param data: Data to send 81 ''' 82 83 passmgr = urllib2.HTTPPasswordMgr() 84 passmgr.add_password(self._realm, self._url, self._username, self._password) 85 86 auth_handler = urllib2.HTTPDigestAuthHandler(passmgr) 87 opener = urllib2.build_opener(auth_handler) 88 urllib2.install_opener(opener) 89 90 req = urllib2.Request(self._url, data, self._headers) 91 92 try: 93 self._fd = urllib2.urlopen(req) 94 except: 95 self._fd = None
96
97 - def receive(self, size = None):
98 ''' 99 Receive upto 10000 bytes of data. 100 101 @rtype: string 102 @return: received data. 103 ''' 104 if self._fd: 105 if size != None: 106 return self._fd.read(size) 107 108 return self._fd.read() 109 110 else: 111 return ''
112
113 -class HttpBasicAuth(Publisher):
114 ''' 115 A simple HTTP publisher. 116 ''' 117
118 - def __init__(self, url, realm, username, password, headers = None, timeout = 0.1):
119 ''' 120 @type host: string 121 @param host: Remote host 122 @type port: number 123 @param port: Remote port 124 @type timeout: number 125 @param timeout: How long to wait for reponse 126 ''' 127 self._url = url 128 self._realm = realm 129 self._username = username 130 self._password = password 131 self._headers = headers 132 self._timeout = float(timeout) 133 self._fd = None
134
135 - def start(self):
136 ''' 137 Create connection. 138 ''' 139 pass
140
141 - def stop(self):
142 self.close()
143
144 - def close(self):
145 ''' 146 Close connection if open. 147 ''' 148 if self._fd: 149 self._fd.close() 150 self._fd = None
151
152 - def send(self, data):
153 ''' 154 Send data via sendall. 155 156 @type data: string 157 @param data: Data to send 158 ''' 159 160 passmgr = urllib2.HTTPPasswordMgr() 161 passmgr.add_password(self._realm, self._url, self._username, self._password) 162 163 auth_handler = urllib2.HTTPBasicAuthHandler(passmgr) 164 opener = urllib2.build_opener(auth_handler) 165 urllib2.install_opener(opener) 166 167 req = urllib2.Request(self._url, data, self._headers) 168 169 try: 170 self._fd = urllib2.urlopen(req) 171 except: 172 self._fd = None
173 174
175 - def receive(self, size = None):
176 ''' 177 Receive upto 10000 bytes of data. 178 179 @rtype: string 180 @return: received data. 181 ''' 182 if self._fd: 183 if size != None: 184 return self._fd.read(size) 185 186 return self._fd.read() 187 188 else: 189 return ''
190 191 # end 192