Ejemplo n.º 1
0
    def do_get(self):  # 请注意之前是do_GET(self)
        """
        """
        ip_dict = {}  # 之前是dict

        parsed_path = urlparse.urlparse(self.path)
        # 定义一个获取成功链接的返回结果
        try:
            query = urllib.unquote(parsed_path.query)
            # print(query)  # 测试输出
            logger.info("query %s" % query)  # 指定query指定给logger
            # 获取返回的url的值
            if query.find('&') != -1:
                param_list = query.split('&')
                for param in param_list:
                    ip_dict[param.split('=')[0]] = param.split('=')[1]
            else:
                    ip_dict[query.split('=')[0]] = query.split('=')[1]

            sql_helper = SqliteHelper()  #
            # 处理删除代理的请求
            if 'delete' in ip_dict:   # 之前的写法dict.has_key('delete')
                condition = "ip='" + ip_dict['ip'] + "' AND port=" + ip_dict['port']
                sql_helper.delete(SqliteHelper.tableName, condition)
                self.send_response(200)
                self.end_headers()
                self.wfile.write("Success delete proxy: " + ip_dict['ip'] + ":" + ip_dict['port'])
            else:
                str_count = ''
                conditions = []
                for key in ip_dict:
                    if key == 'count':
                        str_count = 'LIMIT 0,%s' % ip_dict[key]
                    if key == 'country' or key == 'area':
                        conditions .append(key+" LIKE '"+ip_dict[key]+"%'")
                    elif key == 'types' or key == 'protocol' or key == 'country' or key == 'area':
                        conditions .append(key+"="+ip_dict[key])
                if len(conditions) > 1:
                    conditions = ' AND '.join(conditions)
                else:
                    conditions = conditions[0]
                result = sql_helper.select(sql_helper.tableName, conditions, str_count)
                # print type(result)
                # for r in  result:
                #     print r
                data = [{'ip': item[0], 'port': item[1]} for item in result]  # 返回有效的ip数据
                data = json.dumps(data)  # 生成json数据格式
                self.send_response(200)
                self.end_headers()
                self.wfile.write(data)
        except Exception as e:
            logger.warning(str(e))
            self.send_response(404)
Ejemplo n.º 2
0
    def do_GET(self):
        """
        """
        dict = {}

        parsed_path = urlparse.urlparse(self.path)
        try:
            query = urllib.unquote(parsed_path.query)
            logger.info("query %s" % query)
            if query.find('&') != -1:
                params = query.split('&')
                for param in params:
                    dict[param.split('=')[0]] = param.split('=')[1]
            else:
                dict[query.split('=')[0]] = query.split('=')[1]

            sqlHelper = SqliteHelper()
            # 处理删除代理的请求
            if dict.has_key('delete'):
                condition = "ip='" + dict['ip'] + "' AND port=" + dict['port']
                sqlHelper.delete(SqliteHelper.tableName, condition)
                self.send_response(200)
                self.end_headers()
                self.wfile.write("Success delete proxy: " + dict['ip'] + ":" +
                                 dict['port'])
            else:
                str_count = ''
                conditions = []
                for key in dict:
                    if key == 'count':
                        str_count = 'LIMIT 0,%s' % dict[key]
                    if key == 'country' or key == 'area':
                        conditions.append(key + " LIKE '" + dict[key] + "%'")
                    elif key == 'types' or key == 'protocol' or key == 'country' or key == 'area':
                        conditions.append(key + "=" + dict[key])
                if len(conditions) > 1:
                    conditions = ' AND '.join(conditions)
                else:
                    conditions = conditions[0]
                result = sqlHelper.select(sqlHelper.tableName, conditions,
                                          str_count)
                # print type(result)
                # for r in  result:
                #     print r
                data = [{'ip': item[0], 'port': item[1]} for item in result]
                data = json.dumps(data)
                self.send_response(200)
                self.end_headers()
                self.wfile.write(data)
        except Exception, e:
            logger.warning(str(e))
            self.send_response(404)