Exemplo n.º 1
0
    def get(self, lat, lon):

        lat, lon = float(lat), float(lon)
        lat_100, lon_100 =  int(lat*100+0.5), int(lon*100+0.5)
        key = "e2m:%s" % hashlib.md5("%s%s" % (lat_100, lon_100)).hexdigest()

        off_json = self.rdb.get(key)
        if not off_json:
            t_name = "offset_%s_%s" % (int(lat/3 + 0.5), int(lon/3 + 0.5))
            try:
                entries = self.db.query("SELECT off_x, off_y FROM " + t_name + " WHERE lat=%s AND lon=%s", lat_100, lon_100)
            except Exception:
                entries = None

            if entries:
                off_dict = entries[0]
                self.rdb.set(key, json_encode(off_dict))
            else:
                off_dict = None
        else:
            off_dict = json_decode(off_json)

        fake_lat, fake_lon = lat, lon
        if off_dict:
            op = OffsetPos(lat, lon, off_dict)
            fake_lat, fake_lon = op.getFakePos()

        fake_lat = float(decimal.Decimal(fake_lat).quantize(decimal.Decimal('0.000001')))
        fake_lon = float(decimal.Decimal(fake_lon).quantize(decimal.Decimal('0.000001')))
        res = json_encode({'lat': fake_lat, 'lon': fake_lon})

        self.render_json(res)
Exemplo n.º 2
0
 def post(self):
     user = self.current_user
     do = is_value( self.get_argument("do", "add") )
     tag = is_value(self.get_argument("tag", None) )
     tag_page = is_value(self.get_argument("page", 'tag'))
     group_id = is_value( self.get_argument("group_id", '-1'))
     result = {'kind': -1, 'info': ''} 
     if tag is None or len(tag) > 45:
         result['info'] = '分类字数需在15字以内!'
         self.write( json_encode(result) )
         return
     if tag == "alltags" or tag == "default":
         result['info'] = '不能操作默认分类!'
         self.write( json_encode(result) )
         return
     if tag_page != 'group-tag':
         target = user
     else:
         try:
             #print 'group_id', group_id
             group = BasicGroup(_id=group_id)
             if group.get_member_type(user) != 'Manager':
                 result['info'] = '您无权进行设置!'
                 self.write(json_encode(result))
                 return
         except Exception, e:
             logging.error(traceback.format_exc())
             logging.error('Url Error %s' % self.request.uri)
             
             result['info'] = '小组不存在!'
             self.write(json_encode(result))
             return
         else:
Exemplo n.º 3
0
    def post(self):
        params = {}
        resp = {KEY.STATUS: 500}
        params = utils.decode_params(self.request)
        if KEY.ID not in params or KEY.EVENT_ID not in params or KEY.USER_ID not in params or KEY.VALUE not in params:
            self.write(json_encode(resp))
            return

        '''trans the term's type'''
        params[KEY.ID] = int(params[KEY.ID])
        params[KEY.EVENT_ID] = int(params[KEY.EVENT_ID])
        params[KEY.USER_ID] = int(params[KEY.USER_ID])
        params[KEY.VALUE] = float(params[KEY.VALUE])
        
        flag = db.evaluate_user(params)
        if flag:
            '''we can import here, use some algorithm to incease the helper's reputation'''
            user = db.get_user_information({KEY.ID: params[KEY.USER_ID]})
            if user is not None:
                reputation = user[KEY.REPUTATION]
                params[KEY.VALUE] = reputation*(1 - VALUE.RATE) + VALUE.RATE*params[KEY.VALUE]
                if db.update_user({KEY.ID: params[KEY.USER_ID], KEY.REPUTATION: params[KEY.VALUE]}):
                    resp[KEY.STATUS] = 200

        self.write(json_encode(resp))
Exemplo n.º 4
0
 def get(self):
     word = self.get_argument('word')
     word = word.encode('utf_8')
     c = redis.Redis(host='127.0.0.1', port=6379, db=0)
     tag = c.exists(word)
     #c = database.AsyncRedis.client()
     #tag = yield c.async.exists(word)
     if tag:
         #value = yield c.async.zrange("%s"%word, 0, -1, False)
         value = c.zrange(word, 0, -1)
         
         #c.zincrby(word, value, 1)
         data = []
         value = eval(value[0])
         for li in value:
             songname = li[2]
             singer = li[3]
             album = li[-1]
             url = "commdown('" + "','".join(li) + "');"
             result = {}
             result['songname'] = songname
             result['singer']   = singer
             result['album']    = album
             result['from']     = "kuwo"
             result['url']      = url 
             data.append(result)
         json = {"result":data}
         self.write(json_encode(json))
         #self.write(json_encode(json))
     
     else:
         self.write(json_encode(''))
Exemplo n.º 5
0
 def post(self):
     user = self.current_user
     result = {'kind':1, 'info':''} 
     follow_id = is_value(self.get_argument("id", None))
     do = is_value(self.get_argument("do", 'follow'))
     follow_type = is_value(self.get_argument("follow_type", 'author'))
     
     if follow_id is None or do not in ['follow', 'unfollow'] or follow_id == str(user._id):
         result['info'] = '参数错误!'
         self.write(json_encode(result))
         return
     if follow_type != 'group':      
         try:
             follower = User(_id=follow_id)
         except Exception, e:
             logging.error('User is not exist, in AuthorDoFollowHandler, user id: %s' % follow_id)
             result['info'] = '该用户不存在!'
             self.write(json_encode(result))
             return
         else:
             if do == "follow":
                 user.follow_user(follower)
                 result['kind'] = 0
             elif do == "unfollow":
                 user.unfollow_user(follower)
                 result['kind'] = 0
             self.write(json_encode(result))
             return
Exemplo n.º 6
0
 def on_message(self, m):
   try:
     self._broadcast(m)
     m = json_decode(m)
     logging.info(m)
     if m['method'] == 'getScore':
       package = self._getScore()
       if package: self.send(json_encode(package))
       return
     elif m['method'] == 'addNote':
       package = self._addNote(m['note'])
     elif m['method'] == 'removeNote':
       package = self._removeNote(m['note'])
     elif m['method'] == 'updateNote':
       package = self._updateNote(m['old_note'], m['new_note'])
     elif m['method'] == 'addMeasureBlock':
       package = self._addMeasureBlock(m['measureBlock'])
     elif m['method'] == 'removeMeasureBlock':
       package = self._removeMeasureBlock(m['measureBlock'])
     elif m['method'] == 'clearScore':
       package = self._clearScore()
     else:
       raise ValueError("Didn't specify valid method")
   except Exception, e:
     logging.error(traceback.print_exc())
     self.send(json_encode("Python exception: %s" % str(e)))
Exemplo n.º 7
0
    def post(self):
        params = {}
        resp = {KEY.STATUS: 500}
        params = utils.decode_params(self.request)
        if KEY.EVENT_ID not in params:
            self.write(json_encode(resp))
            return

        '''trans the term's type'''
        params[KEY.EVENT_ID] = int(params[KEY.EVENT_ID])
        supporter_ids = db.list_support_relation(params)
        print supporter_ids
        if supporter_ids is not None:
            supporter_info = []
            for item in supporter_ids:
                user_info = db.get_user_information({KEY.ID: item})
                if user_info is not None:
                    user = {}
                    user[KEY.ID] = user_info[KEY.ID]
                    user[KEY.NICKNAME] = user_info[KEY.NICKNAME]
                    user[KEY.NAME] = user_info[KEY.NAME]
                    user[KEY.OCCUPATION] = user_info[KEY.OCCUPATION]
                    user[KEY.REPUTATION] = user_info[KEY.REPUTATION]
                    user[KEY.LOCATION] = user_info[KEY.LOCATION]
                    user[KEY.IS_VERIFY] = user_info[KEY.IS_VERIFY]
                    user[KEY.GENDER] = user_info[KEY.GENDER]
                    supporter_info.append(user)
            resp[KEY.STATUS] = 200
            resp[KEY.INFO] = supporter_info

        self.write(json_encode(resp))
Exemplo n.º 8
0
 def get(self):
     uri_hash_code= hashlib.md5(self.request.uri).hexdigest()
     res = self.redis_client.get('String:' + uri_hash_code)
     if res: return self.finish(res)
     
     urls = self.get_argument('urls', '')
     start = self.get_argument('start', '')
     end = self.get_argument('end', '')        
     need_query = []
     task_list = json_decode(urls)
     
     for task in task_list:
         mark = task['url'] + '@' + task['host']
         task_id = self.redis_client.hget('Hash:Tan14TaskId', mark)
         if not task_id: task_id = self.mdb['TaskList'].find_one({'task_target': task['url'], 'http_proxy': task['host'] + ':80'}, {'task_id':1, '_id': 0})
         if task_id:
             need_query.append(task_id)
         else:
             task_id = self.add_task(task)
             if task_id: self.redis_client.hset('Hash:Tan14TaskId', mark, task_id)
     if need_query:
         cursor = self.mdb['Result'].find({'task_id': {'$in': need_query}, 
                                           'record_time': {'$gt': int(start), '$lt': int(end)}}, 
                                          {'_id': 0, 'task_id': 1, 'http_code': 1, 'connect_time': 1, 'record_time': 1}).sort([('record_time', 1)])
         if cursor.count() != 0:
             a, b = self.merge_data(list(cursor))
             retval = self.gen_retval(0, a, b)
             self.redis_client.set('String:'+ uri_hash_code, json_encode(retval))
             self.redis_client.expire('String:'+uri_hash_code, 24 * 3600)
         else:
             retval = self.gen_retval(1, a=[], b=[])
     else:
         retval = self.gen_retval(1, a=[], b=[])
     
     self.write(json_encode(retval))
 def get(self, *args):
     url = self.get_argument('url')
     response = yield fetch_webpage(url)
     if response.body:
         (bookmark, webpage) = self.bm.refresh_by_goose(url, html=response.body)
     else:
         print("Error:", response.error)
         self.write(json_encode({'success': 'false'}))
     # 默认使用readability的parser API
     # 教程: http://www.readability.com/developers/api/parser
     # try:
     #     http_client = tornado.httpclient.AsyncHTTPClient()
     #     response = yield http_client.fetch("https://readability.com/api/content/v1/parser?token=7f579fc61973e200632c9e43ff2639234817fbb3&url=" + tornado.escape.url_escape(url))
     #     if response.body:
     #         (bookmark, webpage) = self.bm.refresh_by_readability(url, readability=response.body)
     # except tornado.httpclient.HTTPError:
     #     # print("Error:", response.error)
     #     # 备用,处理readability无法识别的网页
     #     response = yield fetch_webpage(url)
     #     if response.body:
     #         (bookmark, webpage) = self.bm.refresh_by_html(url, html=response.body)
     #     else:
     #         print("Error:", response.error)
     #         self.write(json_encode({'success': 'false'}))
     if bookmark:
         bookmark_module = tornado.escape.to_basestring(
                 self.render_string('modules/bookmark.html', bookmark=bookmark))
         self.write(json_encode({
             'success': 'true',
             'bookmark_module': bookmark_module,
             'title': bookmark['title'],
             'article': tornado.escape.to_basestring(webpage['content'])
         }))
     else:
         self.write(json_encode({'success': 'false'}))
 def get(self, user_id):
     try:
         user_id = user_id.replace('.json', '')
         news_feed = []
         for row in self.db_stories.view('stories/by_user_id', key=user_id, include_docs="true", descending="true", limit=10):
             story = row.doc
             story['story_id'] = story['_id']
             del story['_id']
             del story['_rev']
             del story['session_id']
             if 'media_name' not in story:
                 m = re.match('https\:\/\/.+amazonaws\.com\/(.+)\/(.+)', story['media_url'])
                 if m is not None:
                     story['media_bucket'] = m.group(1)
                     story['media_name'] = m.group(2)
             news_feed.append(story)
         self.write(json_encode({"news_feed":{
                     "response": "true",
                     "status": "success",
                     "news_feed": news_feed,
         }}))
     except Exception:
         self.write(json_encode({"news_feed":{
             "response": "false",
             "status": "internal error",
             "traceback": traceback.format_exc()
         }}))
Exemplo n.º 11
0
 def post(self):
     """Add a facility."""
     global compressed_facilities
     if not revisit_online:
         raise tornado.web.HTTPError(502)
     new_facility = json_decode(self.request.body)
     c_facilities_json = json_decode(compressed_facilities)
     facility_data = (
         c_facilities_json['facilities']['children']['wn']['data'][0]
     )
     uncompressed = json_decode(lzs.decompressFromUTF16(facility_data))
     uncompressed.append({
         '_version': 0,
         'active': True,
         'coordinates': new_facility['coordinates'],
         'createdAt': '2014-04-23T20:32:20.043Z',
         'href': (
             'http://localhost:3000/api/v0/facilities/{}.json'.format(
                 new_facility['uuid']
             )
         ),
         'identifiers': [],
         'name': new_facility['name'],
         'properties': new_facility['properties'],
         'updatedAt': '2014-04-23T20:32:20.043Z',
         'uuid': new_facility['uuid'],
     })
     compressed = lzs.compressToUTF16(json_encode(uncompressed))
     c_facilities_json['facilities']['children']['wn']['data'] = [
         compressed
     ]
     compressed_facilities = json_encode(c_facilities_json).encode()
     self.set_status(201)
Exemplo n.º 12
0
    def post(self):

        category_id = int(self.get_argument('category_id', 0))
        title = self.get_argument('title', '')
        content = self.get_argument('content', '')
        create_uid = self.get_secure_cookie('admin_user_id')
        status = int(self.get_argument('status', 0))

        if not category_id:
            response = {'code': 110, 'msg': self.error_message['110']}
            return self.write(json_encode(response))
        if not title:
            response = {'code': 111, 'msg': self.error_message['111']}
            return self.write(json_encode(response))
        if not content:
            response = {'code': 112, 'msg': self.error_message['112']}
            return self.write(json_encode(response))

        result = News.new(category_id, title, content, create_uid, status)
        if result:
            response = {'code': 0, 'msg': '添加成功'}
            return self.write(json_encode(response))
        else:
            response = {'code': 114, 'msg': self.error_message['114']}
            return self.write(json_encode(response))
Exemplo n.º 13
0
    def post(self):
        params = {}
        resp = {KEY.STATUS: 500}
        params = utils.decode_params(self.request)
        if KEY.EVENT_ID not in params:
            self.write(json_encode(resp))
            return

        '''trans the term's type'''
        params[KEY.EVENT_ID] = int(params[KEY.EVENT_ID])
        launcher_info = db.get_event_information(params)
        if launcher_info is not None:
            user_info = db.get_user_information({KEY.ID: launcher_info[KEY.LAUNCHER_ID]})
            if user_info is not None:
                resp[KEY.STATUS] = 200
                resp[KEY.ID] = user_info[KEY.ID]
                resp[KEY.NICKNAME] = user_info[KEY.NICKNAME]
                resp[KEY.NAME] = user_info[KEY.NAME]
                resp[KEY.AGE] = user_info[KEY.AGE]
                resp[KEY.GENDER] = user_info[KEY.GENDER]
                resp[KEY.PHONE] = user_info[KEY.PHONE]
                resp[KEY.OCCUPATION] = user_info[KEY.OCCUPATION]
                resp[KEY.REPUTATION] = user_info[KEY.REPUTATION]
                resp[KEY.LOCATION] = user_info[KEY.LOCATION]
                resp[KEY.IS_VERIFY] = user_info[KEY.IS_VERIFY]

        self.write(json_encode(resp))
Exemplo n.º 14
0
 def get_valid_locate_or_isp(self, kind = None):
     """
     获取当前可用地区列表
     从locate_mgr获取可用地区列表
     查询RegisteredLocate,获取详细信息
     { "info" : { "data" : { "region_id" : "310000", "region" : "上海市", "isp" : "电信", "isp_id" : "100017" } } }
     """
     if kind in ('region', 'isp'):
         valid_locate = self.loct_mgr.get_valid_locate()
         if valid_locate:
             try:
                 cursor = self.mdb['RegisteredSlave'].find({'locate_key': {'$in': valid_locate}}, 
                                        {'_id'                     : 0, 
                                         'ip_info.data.' + kind       : 1, 
                                         'ip_info.data.' + kind +'_id': 1 })
             except TypeError:
                 return self.write(json_encode({'code': 1, 'data': '数据库查询参数错误'}))
             retval = []
             tmp = {}
             for res in cursor:
                 tmp.setdefault(res['ip_info']['data'][kind], res['ip_info']['data'][kind + '_id'])
             for locate_name, locate_key in tmp.iteritems():
                 retval.append({locate_name: locate_key})
             self.write(json_encode({'code': 0, 'data': retval}))
         else:
             self.write(json_encode({'code': 1, 'data': '当前没有信息'}))
     else:
         raise AssertionError('kind参数只能取值region或isp')
Exemplo n.º 15
0
    def post(self):
        params = {}
        resp = {KEY.STATUS: 500}
        params = utils.decode_params(self.request)
        if KEY.LONGITUDE not in params or KEY.LATITUDE not in params:
            self.write(json_encode(resp))
            return

        '''trans the term's type'''
        params[KEY.LONGITUDE] = float(params[KEY.LONGITUDE])
        params[KEY.LATITUDE] = float(params[KEY.LATITUDE])
        params[KEY.STATE] = 0
        event_id_list = db.get_nearby_event_by_location(params)
        if event_id_list is not None:
            event_list = []
            for item in event_id_list:
                event_info = db.get_event_information({KEY.EVENT_ID: item})
                if event_info is not None:
                    event_temp = {}
                    event_temp[KEY.EVENT_ID] = event_info[KEY.EVENT_ID]
                    event_temp[KEY.TYPE] = event_info[KEY.TYPE]
                    event_temp[KEY.LONGITUDE] = event_info[KEY.LONGITUDE]
                    event_temp[KEY.LATITUDE] = event_info[KEY.LATITUDE]
                    event_list.append(event_temp)
            resp[KEY.STATUS] = 200
            resp[KEY.INFO] = event_list

        self.write(json_encode(resp))
Exemplo n.º 16
0
    def get(self, *args):
        '''
        deneme 2
        :param args:
        :return:
        '''

        (friends_or_followers) = args

        queue_collection = self.application.db.motor_column.queue

        # Get the N'th most frequent visited one
        N = 100

        if friends_or_followers == 'followers':
            cur = queue_collection.find({"protected": {"$ne": True}}).sort('followers_retrieved_at',
                                                                           -1).limit(N)
        else:
            cur = queue_collection.find({"protected": {"$ne": True}}).sort('friends_retrieved_at',
                                                                           -1).limit(N)

        a = []
        for c in (yield cur.to_list(length=100)):
            a.append(c["id"])
            # print c

        idx = random.randint(0, N - 1)
        # TODO: Needs redesign, current strategy too naive
        if len(a) == 0:
            self.write(json_encode(0))
            print "Suggest ToGet %s : User %d " % (friends_or_followers, 0)
        else:
            self.write(json_encode(a[idx]))
            print "Suggest ToGet %s : User %d " % (friends_or_followers, a[-1])
Exemplo n.º 17
0
    def post(self):
        params = {}
        resp = {KEY.STATUS: 500}
        params = utils.decode_params(self.request)
        if KEY.ID not in params or KEY.EVENT_ID not in params or KEY.LAUNCHER_ID not in params or KEY.TYPE not in params:
            self.write(json_encode(resp))
            return

        '''trans the term's type'''
        params[KEY.ID] = int(params[KEY.ID])
        params[KEY.EVENT_ID] = int(params[KEY.EVENT_ID])
        params[KEY.LAUNCHER_ID] = int(params[KEY.LAUNCHER_ID])
        params[KEY.TYPE] = int(params[KEY.TYPE])

        flag = db.add_support_relation(params)
        if flag >= 0:
            resp[KEY.STATUS] = 200
            '''send notification to supportee, someone is coming to help him'''
            launcher_info = db.get_user_information({KEY.ID: params[KEY.LAUNCHER_ID]})
            launcher_info = utils.trans_unicode_to_utf(launcher_info)
            if launcher_info is not None:
                if params[KEY.TYPE] == 2:
                    mess = sendHelp.buildMessage(type=1, title=title, content=content, style=xinge.Style(0, 0, 0, 1, 2))
                    sendHelp.sendEhelp(header + launcher_info[KEY.NICKNAME], mess)
                if params[KEY.TYPE] == 1:
                    mess = sendHelp.buildMessage(type=1, title=title, content=content, style=xinge.Style(0, 0, 0, 0, 5))
                    sendHelp.sendEhelp(header + launcher_info[KEY.NICKNAME], mess)


        self.write(json_encode(resp))
Exemplo n.º 18
0
    def async_get(self, mapnik_map):
        try:
            layer = self.layer_by_id(mapnik_map, self.layer_id)

            field_values = [dict(f).get(self.field_name) for f in layer.datasource.all_features()]

            start = 0 + int(self.get_argument("start", 0))
            end = int(self.get_argument("limit", 30)) + int(self.get_argument("start", 0))

            stringlen = {"key": len} if isinstance(field_values[0], basestring) else {}

            json = json_encode(
                {
                    "min": min(field_values, **stringlen),
                    "max": max(field_values, **stringlen),
                    "count": len(set(field_values)),
                    "field": self.field_name,
                    "values": sorted(list(set(field_values)))[start:end],
                }
            )
            self.jsonp(json, self.get_argument("jsoncallback", None))
        except IndexError:
            self.jsonp({"error": "Layer not found"}, self.get_argument("jsoncallback", None))
        except Exception, e:
            self.jsonp(json_encode({"error": "Exception: %s" % e}), self.get_argument("jsoncallback", None))
Exemplo n.º 19
0
    def get(self, *args):
        '''
        deneme

        :param args:
        :return:
        '''

        (num) = args

        queue_collection = self.application.db.motor_column.queue

        print num
        # if N>100:
        N = 100
        # TODO: Needs redesign, current strategy too naive

        cur = queue_collection.find({"protected": False}).sort('profile_retrieved_at', 1).limit(N)

        a = []
        for c in (yield cur.to_list(length=100)):
            a.append(c["id_str"])
            # print c

        #        idx = random.randint(0, N-1)
        if len(a) == 0:
            self.write(json_encode(0))
        else:
            self.write(json_encode(a))
            print a
Exemplo n.º 20
0
    def post(self):
        params = {}
        resp = {KEY.STATUS: 500}
        params = utils.decode_params(self.request)
        if KEY.EVENT_ID not in params:
            self.write(json_encode(resp))
            return

        '''trans the term's type'''
        params[KEY.EVENT_ID] = int(params[KEY.EVENT_ID])
        event_info = db.get_event_information(params)
        if event_info is not None:
            resp[KEY.STATUS] = 200
            resp[KEY.LAUNCHER_ID] = event_info[KEY.LAUNCHER_ID]
            resp[KEY.TIME] = event_info[KEY.TIME]
            resp[KEY.TYPE] = int(event_info[KEY.TYPE])
            resp[KEY.SUPPORT_NUMBER] = event_info[KEY.SUPPORT_NUMBER]
            resp[KEY.DEMAND_NUMBER] = event_info[KEY.DEMAND_NUMBER]
            resp[KEY.LONGITUDE] = event_info[KEY.LONGITUDE]
            resp[KEY.LATITUDE] = event_info[KEY.LATITUDE]
            resp[KEY.LAUNCHER_NAME] = event_info[KEY.LAUNCHER]
            user = db.get_user_information({KEY.ID: resp[KEY.LAUNCHER_ID]})
            if user is not None:
                resp[KEY.CONTACT] = user[KEY.PHONE]

            '''if this event is help'''
            if resp[KEY.TYPE] == 1:
                resp[KEY.TITLE] = event_info[KEY.TITLE]
                resp[KEY.CONTENT] = event_info[KEY.CONTENT]
                resp[KEY.LOVE_COIN] = int(event_info[KEY.LOVE_COIN])

        self.write(json_encode(resp))
Exemplo n.º 21
0
    def _on_notify_response(self, data, error):

        if data:
            self.write(json_encode(data))
        else:
            self.write(json_encode(""))
        self.finish()
Exemplo n.º 22
0
    def post(self):
        user_id = int(self.get_argument('id', 0))
        username = self.get_argument('username', '')
        password = self.get_argument('password', '')
        email = self.get_argument('email', '')
        realname = self.get_argument('realname', '')
        role_id = int(self.get_argument('role_id', 0))


        match = re.search(r'[\w.-]+@[\w.-]+', email)
        if not match:
            ret = {'code': 112, 'msg': self.error_message['112']}
            return self.write(json_encode(ret))

        if not role_id:
            ret = {'code': 113, 'msg': self.error_message['113']}
            return self.write(json_encode(ret))

        result = User.update(user_id, username, email, password, realname, role_id)
        if result:
            ret = {'code': 0, 'msg': '更新成功'}
            return self.write(json_encode(ret))
        else:
            ret = {'code': 114, 'msg': self.error_message['114']}
            return self.write(json_encode(ret))
Exemplo n.º 23
0
 def post(self):
     data = self.request.arguments
     print data
     timekey= data['timekey'][0]
     s_file_path=data['s_file_path'][0]
     filename=s_file_path.split('/')[-1]
     copyinfo=None
     if "salt://files/" not in s_file_path:
         print "not in salt://"
         if os.path.exists(s_file_path):
             #如果filse目录不存在则创建
             if not os.path.exists('/srv/salt/files'):
                 os.makedirs('/srv/salt/files')
             #复制源文件到salt的files目录
             shutil.copyfile(s_file_path,"/srv/salt/files/%s" %filename)
         else:
             print "no"
             copyinfo= "%s is not exist" % s_file_path
             print copyinfo
     d_file_path=data['d_file_path'][0]
     if not data['devsarr']:
         ress={"status":1,"value":"no devs!"}
         self.write(json_encode(ress))
     #循环设备
     for devid in data['devsarr']:
         info = db.query('select * from host_devs where id='+devid)[0]
         print info
         ip = info['ip']
         print "ip:"+ip
         
         pushfiletask.delay(copyinfo, timekey, ip, filename, s_file_path, d_file_path)
         ress={"status":0,"value":"提交任务成功"}
     self.write(json_encode(ress))
Exemplo n.º 24
0
    async def post(self):
        try:
            args = RouteInputSchema().load(
                escape.json_decode(self.request.body))
        except ValidationError as e:
            raise web.HTTPError(400, escape.json_encode(e.messages),
                                e.messages)
        route_id = uuid.uuid4()
        data = args.data
        new_route = Route(
            id=str(route_id),
            origin=func.ST_GeomFromGeoJSON(escape.json_encode(data['origin'])),
            origin_name=data['origin_name'],
            destination=func.ST_GeomFromGeoJSON(
                escape.json_encode(data['destination'])),
            destination_name=data['destination_name'],
            polyline=func.ST_GeomFromGeoJSON(escape.json_encode(data['polyline'])),
            bounds=data.get('bounds'),
            created=data.get('created') or datetime.utcnow()
        )
        self.db.add(new_route)
        self.db.commit()

        new_route_from_db = self._route_query().filter(
            Route.id == str(route_id)).one()
        schema = RouteOutputSchema()
        output = schema.dumps(row_to_dict(new_route_from_db))
        self.finish(output.data)
Exemplo n.º 25
0
 def post(self):
     data = self.request.arguments 
     print "提交上来的内容"+str(data)
     #print data['shellname'][0]
     shellcontent = data['shellcontent'][0]
     shelltype = data['shelltype'][0]
     timekey= data['timekey'][0]
     if not timekey:
         self.write('no timestamp')
     #根据脚本类型定脚本后缀名称
     scriptname="run.sh"
     if shelltype=="pyhon":
         scriptname="run.py"
     file_object = open("/srv/salt/scripts/%s" % scriptname,"w")
     file_object.write(shellcontent)
     file_object.close()
     os.popen('dos2unix /srv/salt/scripts/%s' % scriptname)
     if not data.has_key('devsarr'):
         ress={"status":1,"value":"no devs"}
         self.write(json_encode(ress))
         return
     #循环设备
     for devid in data['devsarr']:
         #print i
         info = db.query('select * from host_devs where id='+devid)[0]
         print info
         ip = info['ip']
         print "ip:"+ip
         #port=info['port']
         #user = info['user']
         #passwd = info['passwd']
         commandtask.delay(timekey,ip,scriptname)
         ress={"status":0,"value":"任务已提交"}
     self.write(json_encode(ress))
Exemplo n.º 26
0
    def get(self, structure_id):
        print "structure id", structure_id
        if db[PDB_STR_COL_NAME].find_one({"structure_id": structure_id}) == None:
            from crawler import download_pdb_file
            pdb_str = download_pdb_file(structure_id)
            if pdb_str is None:
                #pdb id not found, return 500 code
                self.set_status(500)
                
                self.write(json_encode({
                    "pdb_id": structure_id,
                    "status":"not found"
                }))

            else:
                #find one from pdb bank
                db[PDB_STR_COL_NAME].insert({
                    "structure_id": structure_id,
                    "content": pdb_str
                })
                self.write(json_encode({
                    "pdb_id": structure_id,
                    "status":"inserted"
                }))
        else:
            #it just exists in our db
            self.write(json_encode({
                "pdb_id": structure_id,
                "status":"exist"
            }))
Exemplo n.º 27
0
    def post(self):
        params = {}
        resp = {KEY.STATUS: 500}
        params = utils.decode_params(self.request)
        if KEY.ID not in params or KEY.LAUNCHER_ID not in params or KEY.TYPE not in params or KEY.EVENT_ID not in params:
            self.write(json_encode(resp))
            return

        '''trans the term's type'''
        params[KEY.EVENT_ID] = int(params[KEY.EVENT_ID])
        params[KEY.ID] = int(params[KEY.ID])
        params[KEY.LAUNCHER_ID] = int(params[KEY.LAUNCHER_ID])
        params[KEY.TYPE] = int(params[KEY.TYPE])
        
        flag = db.remove_support_relation(params)
        if flag:
            '''when the event is help or emergence, it is necessary to notify the users'''
            user_info = db.get_user_information({KEY.ID: params[KEY.LAUNCHER_ID]})
            user_info = utils.trans_unicode_to_utf(user_info)
            if user_info is not None:
                if params[KEY.TYPE] == 2:
                    mess = sendHelp.buildMessage(type=1, title=title, content=content, style=xinge.Style(0, 0, 0, 1, 3))
                    sendHelp.sendEhelp(header + user_info[KEY.NICKNAME], mess)
                if params[KEY.TYPE] == 1:
                    mess = sendHelp.buildMessage(type=1, title=title, content=content, style=xinge.Style(0, 0, 0, 0, 6))
                    sendHelp.sendEhelp(header + user_info[KEY.NICKNAME], mess)
            resp[KEY.STATUS] = 200

        self.write(json_encode(resp))
Exemplo n.º 28
0
    def application_id(self) -> ObjectId:
        raw_application_id = self.handler.get_argument("application_id", None)
        if raw_application_id is None:
            self.handler.set_status(428, "missing param(s) application_id")
            self.handler.finish(
                json_encode(
                    {
                        "status": "error",
                        "message": "missing param(s) application_id"
                    }
                )
            )
            raise Finish()

        try:
            return ObjectId(raw_application_id)
        except InvalidId:
            self.handler.set_status(412)
            self.handler.finish(
                json_encode(
                    {
                        "status": "error",
                        "message": "invalid param=application_id,application_id=%s" %
                                   raw_application_id
                    }
                )
            )
            raise Finish()
Exemplo n.º 29
0
 def sign_person_in(self, user):
     """
     If we’ve seen the user before, update their info, otherwise create
     a brand new user record and set their cookie.
     
     """ 
     e_user = self.db.get("SELECT * FROM user WHERE twitter_id = %s", user['id'])
     print "post here"
     if e_user:
         self.db.execute("""UPDATE user 
             SET name=%s, screen_name=%s, description=%s, location=%s, url=%s,
                 profile_image_url=%s, time_zone=%s, access_token=%s, 
                 updated_at=NOW()
             WHERE twitter_id = %s""", user['name'], 
                 user['username'], user['description'], user['location'],
                 user['url'], user['profile_image_url'], user['time_zone'], 
                 json_encode(user['access_token']),e_user['twitter_id'])
     else:
         self.db.execute("""INSERT INTO user (   
                 twitter_id, name, screen_name, description, location, url, 
                 profile_image_url, time_zone, access_token, created_at, updated_at)
             VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), NOW())""",
                 user['id'], user['name'], user['username'], user['description'], user['location'],
                 user['url'], user['profile_image_url'], user['time_zone'],
                 json_encode(user['access_token']))
     
     pid = {'id':user['id'], 'name':user['name'], 'username':user['username']}
     self.set_secure_cookie("pid", tornado.escape.json_encode(pid), expires_days=365)
Exemplo n.º 30
0
    def post(self):
        try:
            request = json_decode(self.request.body)
            req_type = ""
            if "type" in request.keys():
                req_type = request["type"]

            if not req_type:
                self.write_error(406)
                return

            if req_type == "wiki":
                wiki_link = ""
                if "link" in request.keys():
                    wiki_link = request["link"]

                if not core.utils.internet.internet.is_valid_url(wiki_link) or \
                        not wiki_link.startswith("https://en.wikipedia.org/wiki/"):
                    self.write_error(400)
                    return

                all_links = wikilm.WikiLinkMaker.make_links_from_link(wiki_link)
                self.set_header("Content-Type", "application/json")
                self.write(json_encode(all_links))

            else:
                self.set_header("Content-Type", "application/json")
                self.write(json_encode("Oops!"))
        except Exception as e:
            self.set_header("Content-Type", "application/json")
            self.write("Error: " + json_encode(str(e)))
Exemplo n.º 31
0
        try:
            last_time = time.localtime(float(repeat)) 
            current_time = time.localtime()   
        except Exception, e:
            logging.error(traceback.format_exc())
            result['info'] = '参数错误!'
            self.set_cookie("repeat", str(time.time()))
            self.write(json_encode(result))
            return
        else:
            tmp1 = datetime(*last_time[:6])
            tmp2 = datetime(*current_time[:6])
            tmp3 = tmp2 - tmp1
            if tmp3.seconds < 30:
                result['info'] = '时间有误!'
                self.set_cookie("repeat", str(time.time()))
                self.write(json_encode(result))
                return
        result['kind'], result['info'] = fun_repeat_mail(email.lower())
        self.set_cookie("repeat", str(time.time()))
        self.write(json_encode(result))
        return

class InviteHandler(BaseHandler):
    @authfilter
    def get(self):
        return self.render("user-invite.html",title="子曰--邀请注册",user=self.current_user)


        
Exemplo n.º 32
0
def edit_show(show, any_qualities, best_qualities, exceptions_list, location=None, flatten_folders=None, paused=None, direct_call=None, air_by_date=None,
              sports=None, dvdorder=None, indexer_lang=None, subtitles=None, sub_use_sr_metadata=None, skip_downloaded=None, rls_ignore_words=None,
              rls_require_words=None, anime=None, blacklist=None, whitelist=None, scene=None, default_ep_status=None, quality_preset=None, search_delay=None,
              session=None):
    show_obj = find_show(int(show), session=session)

    if not show_obj:
        err_msg = _("Unable to find the specified show: ") + str(show)
        if direct_call:
            sickrage.app.alerts.error(_('Error'), err_msg)
        return False, err_msg

    show_obj.exceptions = get_scene_exceptions(show_obj.indexer_id)

    flatten_folders = not checkbox_to_value(flatten_folders)  # UI inverts this value
    dvdorder = checkbox_to_value(dvdorder)
    skip_downloaded = checkbox_to_value(skip_downloaded)
    paused = checkbox_to_value(paused)
    air_by_date = checkbox_to_value(air_by_date)
    scene = checkbox_to_value(scene)
    sports = checkbox_to_value(sports)
    anime = checkbox_to_value(anime)
    subtitles = checkbox_to_value(subtitles)
    sub_use_sr_metadata = checkbox_to_value(sub_use_sr_metadata)

    if indexer_lang and indexer_lang in IndexerApi(show_obj.indexer).indexer().languages.keys():
        indexer_lang = indexer_lang
    else:
        indexer_lang = show_obj.lang

    # if we changed the language then kick off an update
    if indexer_lang == show_obj.lang:
        do_update = False
    else:
        do_update = True

    if scene == show_obj.scene or anime == show_obj.anime:
        do_update_scene_numbering = False
    else:
        do_update_scene_numbering = True

    show_obj.paused = paused
    show_obj.scene = scene
    show_obj.anime = anime
    show_obj.sports = sports
    show_obj.subtitles = subtitles
    show_obj.sub_use_sr_metadata = sub_use_sr_metadata
    show_obj.air_by_date = air_by_date
    show_obj.default_ep_status = int(default_ep_status)
    show_obj.skip_downloaded = skip_downloaded

    # If directCall from mass_edit_update no scene exceptions handling or blackandwhite list handling
    if direct_call:
        do_update_exceptions = False
    else:
        if set(exceptions_list) == set(show_obj.exceptions):
            do_update_exceptions = False
        else:
            do_update_exceptions = True

        if anime:
            if whitelist:
                shortwhitelist = short_group_names(whitelist)
                show_obj.release_groups.set_white_keywords(shortwhitelist)
            else:
                show_obj.release_groups.set_white_keywords([])

            if blacklist:
                shortblacklist = short_group_names(blacklist)
                show_obj.release_groups.set_black_keywords(shortblacklist)
            else:
                show_obj.release_groups.set_black_keywords([])

    warnings, errors = [], []

    new_quality = try_int(quality_preset, None)
    if not new_quality:
        new_quality = Quality.combine_qualities(list(map(int, any_qualities)), list(map(int, best_qualities)))

    show_obj.quality = new_quality

    # reversed for now
    if bool(show_obj.flatten_folders) != bool(flatten_folders):
        show_obj.flatten_folders = flatten_folders
        try:
            sickrage.app.show_queue.refresh_show(show_obj.indexer_id, True)
        except CantRefreshShowException as e:
            errors.append(_("Unable to refresh this show: {}").format(e))

    if not direct_call:
        show_obj.lang = indexer_lang
        show_obj.dvdorder = dvdorder
        show_obj.rls_ignore_words = rls_ignore_words.strip()
        show_obj.rls_require_words = rls_require_words.strip()
        show_obj.search_delay = int(search_delay)

    # if we change location clear the db of episodes, change it, write to db, and rescan
    if os.path.normpath(show_obj.location) != os.path.normpath(location):
        sickrage.app.log.debug(os.path.normpath(show_obj.location) + " != " + os.path.normpath(location))
        if not os.path.isdir(location) and not sickrage.app.config.create_missing_show_dirs:
            warnings.append("New location {} does not exist".format(location))

        # don't bother if we're going to update anyway
        elif not do_update:
            # change it
            try:
                show_obj.location = location
                try:
                    sickrage.app.show_queue.refresh_show(show_obj.indexer_id, True)
                except CantRefreshShowException as e:
                    errors.append(_("Unable to refresh this show:{}").format(e))
                    # grab updated info from TVDB
                    # showObj.loadEpisodesFromIndexer()
                    # rescan the episodes in the new folder
            except NoNFOException:
                warnings.append(
                    _("The folder at %s doesn't contain a tvshow.nfo - copy your files to that folder before "
                      "you change the directory in SiCKRAGE.") % location)

    # force the update
    if do_update:
        try:
            sickrage.app.show_queue.update_show(show_obj.indexer_id, force=True)
        except CantUpdateShowException as e:
            errors.append(_("Unable to update show: {}").format(e))

    if do_update_exceptions:
        try:
            update_scene_exceptions(show_obj.indexer_id, exceptions_list)
        except CantUpdateShowException:
            warnings.append(_("Unable to force an update on scene exceptions of the show."))

    if do_update_scene_numbering:
        try:
            xem_refresh(show_obj.indexer_id, show_obj.indexer, True)
        except CantUpdateShowException:
            warnings.append(_("Unable to force an update on scene numbering of the show."))

    if direct_call:
        return True if len(warnings) == 0 and len(errors) == 0 else False, json_encode({'warnings': warnings, 'errors': errors})

    if len(warnings) > 0:
        sickrage.app.alerts.message(
            _('{num_warnings:d} warning{plural} while saving changes:').format(num_warnings=len(warnings),
                                                                               plural="" if len(
                                                                                   warnings) == 1 else "s"),
            '<ul>' + '\n'.join(['<li>{0}</li>'.format(warning) for warning in warnings]) + "</ul>")

    if len(errors) > 0:
        sickrage.app.alerts.error(
            _('{num_errors:d} error{plural} while saving changes:').format(num_errors=len(errors),
                                                                           plural="" if len(errors) == 1 else "s"),
            '<ul>' + '\n'.join(['<li>{0}</li>'.format(error) for error in errors]) + "</ul>")

    return True, ""
Exemplo n.º 33
0
 def queue_message(self, msg, **data):
     data["msg"] = msg
     if not self.client_closed:
         self.write_message(json_encode(data), False)
Exemplo n.º 34
0
 def send_message(self, msg, **data):
     """Sends a JSON message to the client."""
     data["msg"] = msg
     if not self.client_closed:
         self.write_message(json_encode(data))
Exemplo n.º 35
0
 def get(self):
     users = UserModel.get_users_by_merchant_id(
         self.db, self.current_user.merchant_id)
     self.render("userManage.html",
                 users=json_encode([user.todict() for user in users]))
Exemplo n.º 36
0
    def test_post_library_pool_process_handler(self):
        # Amplicon test
        data = {
            'plates-info':
            json_encode([{
                'plate-id': 23,
                'pool-func': 'amplicon',
                'dna-amount-23': 240,
                'min-val-23': 1,
                'max-val-23': 15,
                'blank-val-23': 2,
                'epmotion-23': 10,
                'dest-tube-23': 1
            }])
        }
        response = self.post('/process/poollibraries', data)
        self.assertEqual(response.code, 200)
        obs = json_decode(response.body)
        self.assertEqual(len(obs), 1)
        self.assertCountEqual(obs[0], ['plate-id', 'process-id'])

        # Shotgun test
        data = {
            'plates-info':
            json_encode([{
                'plate-id': 26,
                'pool-func': 'equal',
                'volume-26': 200,
                'lib-size-26': 500
            }])
        }
        response = self.post('/process/poollibraries', data)
        self.assertEqual(response.code, 200)
        obs = json_decode(response.body)
        self.assertEqual(len(obs), 1)
        self.assertCountEqual(obs[0], ['plate-id', 'process-id'])

        # Failure amplicon
        data = {
            'plates-info':
            json_encode([{
                'plate-id': 23,
                'pool-func': 'amplicon',
                'dna-amount-23': 240,
                'min-val-23': 1,
                'max-val-23': 15,
                'blank-val-23': 2,
                'epmotion-23': 10
            }])
        }
        response = self.post('/process/poollibraries', data)
        self.assertEqual(response.code, 400)

        # Failure shotgun
        data = {
            'plates-info':
            json_encode([{
                'plate-id': 26,
                'pool-func': 'equal',
                'volume-26': 200
            }])
        }
        response = self.post('/process/poollibraries', data)
        self.assertEqual(response.code, 400)
Exemplo n.º 37
0
 def send_message_to_client(self, message):
     self.write_message(json_encode(message))
Exemplo n.º 38
0
 def content_json(self):
     if not self._content_json:
         self._content_json = json_encode(self.content)
     return self._content_json
Exemplo n.º 39
0
 def get(self, _):
     # self.set_statues(404)
     self.write(json_encode(result_utils.error('20009')))
Exemplo n.º 40
0
 def metadata_json(self):
     if not self._metadata_json:
         self._metadata_json = json_encode(self.metadata)
     return self._metadata_json
Exemplo n.º 41
0
    async def post(self):
        class RequestSchema(Schema):
            date = fields.Date(required=True)

        RequestSchema().loads(self.request.body)
        self.write(json_encode({"test": "ok"}))
Exemplo n.º 42
0
 def header_json(self):
     if not self._header_json:
         self._header_json = json_encode(self.header)
     return self._header_json
Exemplo n.º 43
0
 def write_error(self, status_code: int, **kwargs: Any) -> None:
     code, data = handle_error(kwargs["exc_info"][1], processor)
     self.set_status(code)
     self.write(json_encode(data))
     return
Exemplo n.º 44
0
    def send_message(self, *args, **kwargs):
        callback = kwargs.get('callback', None)
        data = kwargs.get('data', None)

        logger.debug(data)


        # Catch invalid recipient
        if data['recipient'] not in self.connections:
            error = 'Connection "'+data['recipient']+'" not found'
            logger.error(error)

            error = {
                'message': error
            }
            if (callback):
                callback(False, error)
            else:
                return error

        # Sending of an error
        if 'error' in data:
            message = {
                'jsonrpc': '2.0',
                'error': data['error']
            }

        # Sending of a regular message
        else:
            message = {
                'jsonrpc': '2.0',
                'method': data['method'] if 'method' in data else None
            }
            if 'id' in data:
                message['id'] = data['id']
            if 'params' in data:
                message['params'] = data['params']
            if 'result' in data:
                message['result'] = data['result']

        # Dispatch the message
        try:
            self.connections[data['recipient']]['connection'].write_message(json_encode(message))

            response = {
                'message': 'Sent message to '+data['recipient']
            }
            if (callback):
                callback(response)
            else:
                return response
        except:
            error = 'Failed to send message to '+ data['recipient']
            logger.error(error)

            error = {
                'message': error
            }
            if (callback):
                callback(False, error)
            else:
                return error
Exemplo n.º 45
0
 def post(self):
     """
     jobid:
     jobname:
     status:
     message: []
     :return:
     """
     body = json_decode(self.request.body)
     job_id = body.get('jobid', '')
     task_id = body.get('taskid', '')
     task_name = body.get('jobname', '')
     task_status = body.get('status', '')
     task_message = body.get('messages', [])
     if job_id == '' or task_name == '' or task_status == '' or task_message == []:
         res = {
             "status": JobCallbackResponseStatus.fail.value,
             "message": "some argument is null"
         }
         logger.error(
             "job callback fail: {}".format("some argument is null"))
         self.write(json.dumps(res))
         self.finish()
     else:
         logger.info(
             'Job_ID: {}, Task_id: {}, Job_Step: {}, Task_Status: {}'.
             format(job_id, task_id, task_name, task_status))
         zk = self.application.zk
         if zk.update_callback_by_taskid(job_id, task_id, task_status,
                                         task_message):
             logger.info(
                 "update callback by taskid sucess: jobid={}, taskid={}".
                 format(job_id, task_id))
         else:
             logger.error(
                 "update callback by taskid failed: jobid={}, taskid={}".
                 format(job_id, task_id))
         for message in task_message:
             logger.info('"Host": {}, "status": {}, "message": {}'.format(
                 message['host'], message['status'], message['message']))
         if zk.handler_task(job_id, task_id, task_name, task_message,
                            task_status):
             logger.info("handler task success after callback")
             if zk.is_exist_signal(job_id):
                 zk.send_signal(job_id)
             res = {
                 "status":
                 JobCallbackResponseStatus.success.value,
                 "message":
                 "callback receive success, and handler task success after callback"
             }
         else:
             logger.error("handler task fail after callback")
             res = {
                 "status":
                 JobCallbackResponseStatus.success.value,
                 "message":
                 "callback receive success, but handler task fail after callback"
             }
         self.write(json_encode(res))
         self.finish()
Exemplo n.º 46
0
 async def get(self):
     self.write(json_encode({"test": "ok"}))
Exemplo n.º 47
0
 def get(self):
     users = UserModel.get_all()
     self.write(json_encode(users))
Exemplo n.º 48
0
    def get(self, *args, **kwargs):
        codes = [{"id": code, "name": Subtitles().name_from_code(code)} for code in Subtitles().subtitle_code_filter()]
        codes = list(filter(lambda code: code['id'] in Subtitles().wanted_languages(), codes))

        return self.write(json_encode(codes))
Exemplo n.º 49
0
    def update_clients(self):
        json_status = json_encode(self.get_status())

        for client in clients:
            client.write_message(json_status)
Exemplo n.º 50
0
 def post(self):
     name = self.get_argument('name')
     age = self.get_argument('age')
     UserModel.create(name, age)
     resp = {'status': True, 'msg': 'create success'}
     self.write(json_encode(resp))
Exemplo n.º 51
0
 def get(self):
     response = Setting.gets()
     return self.write(json_encode(response))
Exemplo n.º 52
0
 def update(self):
     json_status = json_encode(self.get_status())
     self.write_message(json_status)
Exemplo n.º 53
0
def message(status, content):
    return json_encode(dict(status_code=status, msg=content))
Exemplo n.º 54
0
 def on_timeout(self, num):
     self.time_handler = None
     self.send_data(json_encode({'name': '', 'msg': ''}))
     if (self.client.connection.connected()):
         self.client.disconnect()
Exemplo n.º 55
0
 def write_json(self, data):
     return self.send(escape.json_encode(data))
Exemplo n.º 56
0
def error(status, content):
    return json_encode(dict(status_code=status, err=content))
Exemplo n.º 57
0
 def set_current_user(self, username):
     print "Set current user: "******"user", escape.json_encode(username))
     else:
         self.clear_cookie("user")
Exemplo n.º 58
0
    def request(self,
                method,
                path,
                data=None,
                json=False,
                files=None,
                cookies=None,
                **kwgs):

        kwgs['method'] = method

        ## `path` should be utf-8 encoded string to complete requote process
        #if isinstance(path, str):
        #    path = path.encode('utf8')
        path = requote_uri(path)

        # `body` must be passed if method is one of those three
        if method in ['POST', 'PUT', 'PATCH']:
            headers = kwgs.setdefault('headers', {})
            body = ''
            if files:
                boundary = '1234567890'
                headers[
                    'Content-Type'] = 'multipart/form-data; boundary=%s' % boundary
                L = []
                if data:
                    for k, v in data.items():
                        L.append('--' + boundary)
                        L.append('Content-Disposition: form-data; name="%s"' %
                                 k)
                        L.append('')
                        L.append(v)
                for k, f in files.items():
                    L.append('--' + boundary)
                    L.append(
                        'Content-Disposition: form-data; name="%s"; filename="%s"'
                        % (k, f[0]))
                    L.append('Content-Type: %s' % mimetypes.guess_type(f[0])[0]
                             or 'application/octet-stream')
                    L.append('')
                    L.append(f[1])
                L.append('--%s--' % boundary)
                L.append('')
                body = '\r\n'.join(L)
            else:
                if data:
                    if json:
                        body = json_encode(data)
                        headers['Content-Type'] = 'application/json'
                    else:
                        headers[
                            'Content-Type'] = 'application/x-www-form-urlencoded'
                        body = urlencode(data)
            kwgs['body'] = body
        else:
            if data:
                path = '%s?%s' % (path, urlencode(data))

        if cookies:
            self._add_cookies(cookies, kwgs)

        #print 'fetch kwgs', kwgs
        url = self.get_url(path)
        logging.debug('testing fetch url: %s', url)
        self.http_client.fetch(url, self.stop, **kwgs)
        resp = self.wait()

        self._parse_cookies(resp)

        return resp
Exemplo n.º 59
0
 def get(self):
     self.file_lister.refresh()
     self.set_header('Cache-Control',
                     'no-store, no-cache, must-revalidate, max-age=0')
     self.set_header('Content-Type', 'application/json')
     self.write(escape.json_encode(self.file_lister.files))
Exemplo n.º 60
0
 def return_json(self, data):
     _json_data = json_encode(data)
     self.set_header("Content-Type", "application/json; charset=UTF-8")
     self.write(_json_data)