def send(self, method, uri, body=None, headers={}, bodyType=None): '''Sends a HTTP request with p_method, for p_uri.''' conn = http.client.HTTPConnection(self.host, self.port) try: conn.connect() except(socket.gaierror, sge): raise ResourceError('Check your Internet connection (%s)'% str(sge)) except(socket.error, se): raise ResourceError('Connection error (%s)' % str(se)) # Tell what kind of HTTP request it will be. conn.putrequest(method, uri, skip_host=True) # Add HTTP headers self.updateHeaders(headers) if self.headers: headers.update(self.headers) for n, v in headers.items(): conn.putheader(n, v) conn.endheaders() # Add HTTP body if body: if not bodyType: bodyType = 'string' copyData(body, conn, 'send', type=bodyType) # Send the request, get the reply if self.measure: startTime = time.time() response = conn.getresponse() if self.measure: endTime = time.time() body = response.read() conn.close() # Return a smart object containing the various parts of the response duration = None if self.measure: duration = endTime - startTime self.serverTime += duration return HttpResponse(response, body, duration=duration, utf8=self.utf8)
raise ResourceError('Check your Internet connection (%s)' % str(sge)) except socket.error, se: raise ResourceError('Connection error (%s)' % str(se)) # Tell what kind of HTTP request it will be. conn.putrequest(method, uri) # Add HTTP headers self.updateHeaders(headers) if self.headers: headers.update(self.headers) for n, v in headers.items(): conn.putheader(n, v) conn.endheaders() # Add HTTP body if body: if not bodyType: bodyType = 'string' copyData(body, conn, 'send', type=bodyType) # Send the request, get the reply if self.measure: startTime = time.time() code, text, headers = conn.getreply() if self.measure: endTime = time.time() body = conn.getfile().read() conn.close() # Return a smart object containing the various parts of the response duration = None if self.measure: duration = endTime - startTime self.serverTime += duration return HttpResponse(code, text, headers, body, duration=duration) def mkdir(self, name): '''Creates a folder named p_name in this resource.'''
conn.connect(self.host, self.port) except socket.gaierror, sge: raise ResourceError('Check your Internet connection (%s)'% str(sge)) except socket.error, se: raise ResourceError('Connection error (%s)'% str(sge)) # Tell what kind of HTTP request it will be. conn.putrequest(method, uri) # Add HTTP headers self.updateHeaders(headers) if self.headers: headers.update(self.headers) for n, v in headers.items(): conn.putheader(n, v) conn.endheaders() # Add HTTP body if body: if not bodyType: bodyType = 'string' copyData(body, conn, 'send', type=bodyType) # Send the request, get the reply if self.measure: startTime = time.time() code, text, headers = conn.getreply() if self.measure: endTime = time.time() body = conn.getfile().read() conn.close() # Return a smart object containing the various parts of the response duration = None if self.measure: duration = endTime - startTime self.serverTime += duration return HttpResponse(code, text, headers, body, duration=duration) def mkdir(self, name): '''Creates a folder named p_name in this resource.'''