def process(self): """读取配置文件""" self.cf.read("user.properties") """读取消息主题""" topic = self.cf.get("property", "Topic") """存储消息URL路径""" url = self.cf.get("property", "URL") """访问码""" ak = self.cf.get("property", "Ak") """密钥""" sk = self.cf.get("property", "Sk") """Producer ID""" pid = self.cf.get("property", "ProducerID") """Tag""" tag = self.cf.get("property", "Tag") """Key""" key = self.cf.get("property", "Key") """Content""" content = self.cf.get("property", "Content") """HTTP请求主体内容""" content = U"" + content + "".encode('utf-8') """分隔符""" newline = "\n" """获取URL域名地址""" urlname = urlparse(url).hostname """根据HTPP主体内容计算MD5值""" self.md5.update(content) """建立HTTP连接对象""" conn = httplib.HTTPConnection(parseURL(urlname)) try: for index in range(0, 1): """时间戳""" date = repr(int(time.time() * 1000))[0:13] """构造签名字符串""" signString = str(topic + newline + pid + newline + self.md5.hexdigest() + newline + date) """计算签名""" sign = calSignature(signString, sk) """内容类型""" contentFlag = "Content-type" """HTTP请求头部对象""" headers = { self.signature: sign, self.ak: ak, self.producerid: pid, contentFlag: "text/html;charset=UTF-8" } """开始发送HTTP请求消息""" conn.request(method="POST", url=url + "/message/?topic=" + topic + "&time=" + date + "&tag=" + tag + "&key=" + key, body=content, headers=headers) """获取HTTP应答消息""" response = conn.getresponse() """读取HTTP应答内容""" msg = response.read() print "response:" + msg except Exception, e: print e
def process(self): """读取配置文件""" self.cf.read("user.properties") """读取消息主题""" topic = self.cf.get("property","Topic") """存储消息URL路径""" url = self.cf.get("property","URL") """访问码""" ak = self.cf.get("property","Ak") """密钥""" sk = self.cf.get("property","Sk") """Producer ID""" pid = self.cf.get("property","ProducerID") """HTTP请求主体内容""" content = U"helloworld".encode('utf-8') """分隔符""" newline = "\n" """获取URL域名地址""" urlname = urlparse(url).hostname """根据HTPP主体内容计算MD5值""" self.md5.update(content) """建立HTTP连接对象""" conn = httplib.HTTPConnection(parseURL(urlname)) try: for index in range(0,10): """时间戳""" date = repr(int(time.time() * 1000))[0:13] """构造签名字符串""" signString = str(topic + newline + pid + newline + self.md5.hexdigest() + newline + date) """计算签名""" sign = calSignature(signString,sk) """内容类型""" contentFlag ="Content-type" """HTTP请求头部对象""" headers = { self.signature : sign, self.ak : ak, self.producerid : pid, contentFlag : "text/html;charset=UTF-8" } """开始发送HTTP请求消息""" conn.request(method="POST",url=url + "/message/?topic="+topic+"&time="+date+"&tag=http&key=http", body=content, headers=headers) """获取HTTP应答消息""" response = conn.getresponse() """读取HTTP应答内容""" msg = response.read() print "response:"+msg time.sleep(1) except Exception,e: print e
def process(self): """开始读取配置文件""" self.cf.read("user.properties") """读取主题""" topic = self.cf.get("property", "Topic") """存储消息的URL路径""" url = self.cf.get("property", "URL") """访问码""" ak = self.cf.get("property", "Ak") """密钥""" sk = self.cf.get("property", "Sk") """Consumer ID""" cid = self.cf.get("property", "ConsumerID") newline = "\n" """获取URL主机域名地址""" urlname = urlparse(url).hostname """连接存储消息的服务器""" conn = httplib.HTTPConnection(parseURL(urlname)) while True: try: """时间戳""" date = repr(int(time.time() * 1000))[0:13] """构造签名字符串""" signString = topic + newline + cid + newline + date """计算签名值""" sign = calSignature(signString,sk) """请求消息HTTP头部""" headers = { self.signature : sign, self.ak : ak, self.consumerid : cid } """开始发送获取消息的HTTP请求""" conn.request(method="GET",url=url+"/message/?topic="+topic+"&time="+date+"&num=32&tag=http",headers=headers) """获取HTTP应答消息""" response = conn.getresponse() # print response """验证应答消息状态值""" if response.status != 200: continue """从应答消息中读取实际的消息内容""" msg = response.read() """将实际的消费消息进行解码""" messages = json.loads(msg) if len(messages) == 0: time.sleep(2) continue """依次获取每条消费消息""" for message in messages: print message """计算时间戳""" date = repr(int(time.time() * 1000))[0:13] """构建删除消费消息URL路径""" delUrl = url + "/message/?msgHandle="+message['msgHandle'] + "&topic="+topic+"&time="+date """构造签名字符串""" signString = topic + newline + cid + newline + message['msgHandle'] + newline + date """进行签名""" sign = calSignature(signString,sk) """构造删除消费消息HTTP头部""" delheaders = { self.signature : sign, self.ak : ak, self.consumerid : cid, } """发送删除消息请求""" conn.request(method="DELETE", url=delUrl, headers=delheaders) """获取请求应答""" response = conn.getresponse() """读取应答内容""" #print response msg = response.read() #print "delete msg:"+msg except Exception,e: print e