def request(self, host, handler, request_body, verbose=0): if not self.connection: self.connection = self._get_connection() self.headers = { "User-Agent" : self.user_agent, "Content-Type" : "text/xml", "Accept": "text/xml" } # basic auth if self.username is not None and self.password is not None: unencoded = "%s:%s" % (self.username, self.password) encoded = as_string(encodestring(as_bytes(unencoded))) encoded = encoded.replace('\n', '') encoded = encoded.replace('\012', '') self.headers["Authorization"] = "Basic %s" % encoded self.headers["Content-Length"] = str(len(request_body)) self.connection.request('POST', handler, request_body, self.headers) r = self.connection.getresponse() if r.status != 200: self.connection.close() self.connection = None raise xmlrpclib.ProtocolError(host + handler, r.status, r.reason, '' ) data = r.read() p, u = self.getparser() p.feed(data) p.close() return u.close()
def apply_hash(self, s): """Apply MD5 to a string <s>, then wrap it in base64 encoding.""" m = md5() m.update(s) d = m.digest() # base64.encodestring tacks on an extra linefeed. return encodestring(d)[:-1]
def request(self, host, handler, request_body, verbose=0): if not self.connection: self.connection = self._get_connection() self.headers = { "User-Agent": self.user_agent, "Content-Type": "text/xml", "Accept": "text/xml" } # basic auth if self.username is not None and self.password is not None: unencoded = "%s:%s" % (self.username, self.password) encoded = as_string(encodestring(as_bytes(unencoded))) encoded = encoded.replace('\n', '') encoded = encoded.replace('\012', '') self.headers["Authorization"] = "Basic %s" % encoded self.headers["Content-Length"] = str(len(request_body)) self.connection.request('POST', handler, request_body, self.headers) r = self.connection.getresponse() if r.status != 200: self.connection.close() self.connection = None raise xmlrpclib.ProtocolError(host + handler, r.status, r.reason, '') data = r.read() p, u = self.getparser() p.feed(data) p.close() return u.close()
def handle_connect(self): self.connected = 1 method = "GET" version = "HTTP/1.1" self.push("%s %s %s" % (method, self.path, version)) self.push(CRLF) self.header("Host", self.host) self.header('Accept-Encoding', 'chunked') self.header('Accept', '*/*') self.header('User-agent', self.user_agent) if self.password: auth = '%s:%s' % (self.username, self.password) auth = as_string(encodestring(as_bytes(auth))).strip() self.header('Authorization', 'Basic %s' % auth) self.push(CRLF) self.push(CRLF)
def request(self, host, handler, request_body, verbose=0): # print('request---------------------------------------------') # print('response---------------------------------------------') request_body = as_bytes(request_body) # 请求的时候添加头部文件(http协议的头部文件) if not self.connection: self.connection = self._get_connection() self.headers = { "User-Agent": self.user_agent, "Content-Type": "text/xml", "Accept": "text/xml" } # basic auth if self.username is not None and self.password is not None: unencoded = "%s:%s" % (self.username, self.password) encoded = as_string(encodestring(as_bytes(unencoded))) encoded = encoded.replace('\n', '') encoded = encoded.replace('\012', '') self.headers["Authorization"] = "Basic %s" % encoded self.headers["Content-Length"] = str(len(request_body)) self.connection.request('POST', handler, request_body, self.headers) r = self.connection.getresponse() # print('xml test ') if r.status != 200: self.connection.close() self.connection = None raise xmlrpclib.ProtocolError(host + handler, r.status, r.reason, '') data = r.read() data = as_string(data) # on 2.x, the Expat parser doesn't like Unicode which actually # contains non-ASCII characters data = data.encode('ascii', 'xmlcharrefreplace') p, u = self.getparser() p.feed(data) p.close() # u.close() 对html文档进行解析,返回数组内字典格式 return u.close()