zoukankan      html  css  js  c++  java
  • Python httpsqs封装类

    '''
    httpsqs队列封装
    @author xp_go@qq.com
    
    a = HttpsqsClient('192.168.0.218','1218','httpsqsmmall.com')
    print a.put('logtest','sdfsfsf')
    print a.get('logtest')
    '''
    
    import urllib,urllib2,json
    
    class HttpsqsClient(object):
        def __init__(self,host= '127.0.0.1',port='1218',auth = '', charset = 'utf-8'):
            self.httpsqs_url = 'http://' + host + ':' + port + '/?'
            self.httpsqs_auth = auth
            self.httpsqs_charset = charset
        
        
        def __http_get(self,params):
            '''
            http get??
            params = {"wd":"a","b":"2"}
            '''        
            params['auth'] = self.httpsqs_auth
            params['charset'] = self.httpsqs_charset
            try:
                url_params = urllib.urlencode(params)
                final_url = self.httpsqs_url + url_params
                page = urllib2.urlopen(final_url,timeout=3).read()
                return page
            except urllib2.HTTPError, e:
                print "Error Code:", e.code  
            except urllib2.URLError, e:  
                print "Error Reason:", e.reason
        
        def __http_post(self,params,post_data):
            """
            http post??
            @param params: params = {"wd":"a","b":"2"}
            @param post_data: json or string
            @type post_data: string
            @return:string
            """
            params['auth'] = self.httpsqs_auth
            params['charset'] = self.httpsqs_charset 
                   
            url_params = urllib.urlencode(params)
            final_url = self.httpsqs_url + url_params
            post_data = post_data.encode('utf8')
            try:
                req = urllib2.Request(final_url, post_data)
                page = urllib2.urlopen(req,timeout=3).read()
                return page
            except urllib2.HTTPError, e:  
                print "Error Code:", e.code  
            except urllib2.URLError, e:  
                print "Error Reason:", e.reason
        
        
        def put(self,queue_name,queue_data):
            params = {'opt':'put','name':queue_name}
            r = self.__http_post(params,queue_data)
            if r == 'HTTPSQS_PUT_OK':
                return True
            return False
            
            
        def get(self,queue_name):
            params = {'opt':'get','name':queue_name}
            r = self.__http_get(params)
            if r == 'HTTPSQS_GET_END':
                return None
            return r
    
        def status(self,queue_name):
            params = {'opt':'status','name':queue_name}
            return self.__http_get(params)
    
        def status_json(self,queue_name):
            params = {'opt':'status_json','name':queue_name}
            return self.__http_get(params)
    
        def reset(self,queue_name):
            params = {'opt':'reset','name':queue_name}
            return self.__http_get(params)
    
        def maxqueue(self,queue_name,num):
            params = {'opt':'maxqueue','name':queue_name,'num':str(num)}
            return self.__http_get(params)
    
        def synctime(self,queue_name,num):
            params = {'opt':'synctime','name':queue_name,'num':str(num)}
            return self.__http_get(params)
  • 相关阅读:
    ADO连接access和oracle的一个区别
    我的大救星——Oracle APEX 快速Web开发(鼠标点点即可开发出专业级Web应用)
    Sql Server 2008 R2 error:40 错误处理
    我的第一个WPF程序
    开源社会网络分析工具NodeXL介绍
    JavaScript学习笔记1之基础知识点
    JavaScript学习笔记6 之经典神坑题整理
    JavaScript学习笔记2之Tab切换
    变量声明和函数声明会提升到当前作用域顶部
    JavaScript学习笔记5 之 计时器 & scroll、offset、client系列属性 & 图片无缝滚动
  • 原文地址:https://www.cnblogs.com/seans/p/3820444.html
Copyright © 2011-2022 走看看