def upload_record_without_photo(self, record): """ 不带自拍,上传跑步记录 """ abstract = sha256( f'{self.studentID}_{record.date}_781f5106781f5106'.encode( 'utf-8')).hexdigest()[:32] m = MultipartEncoder( fields={ 'userId': str(self.studentID), 'duration': str(record.duration), 'date': str(record.date), # 后端好像会根据日期来判断是否重复发包 'detail': json.dumps(record.detail), # 路径似乎并不会用来查重 'misc': json.dumps({"agent": "Android v1.2+"}), 'step': str(record.step), 'abstract': abstract, }) # self.logger.debug(record.__dict__) # self.logger.debug(m.to_string()) # 注意! m.to_string() 只能输出一次,第二次将会输出空字节,因此不要用这个方法来调试! # return respJson = self.post("record/{userId}".format(userId=self.studentID), data=m.to_string(), headers={'Content-Type': m.content_type}, auth=self.auth) if not respJson["data"]["verified"]: raise PKURunnerNotVerifiedError( "record is not verified, check your running params setting.")
def emit_many(self, tuples, stream=None, anchors=[], direct_task=None): """A more efficient way to send many tuples, dumps out all tuples to STDOUT instead of writing one at a time. :param tuples: an iterable of ``list``s representing the tuples to send to Storm. All tuples should contain only JSON-serializable data. :param stream: a ``str`` indicating the ID of the steram to emit these tuples to. Specify ``None`` to emit to default stream. :param anchors: a `` list`` of IDs the tuples these tuples should be anchored to. :param direct_task: an ``int`` which indicates the task to send the tuple to. """ if _ANCHOR_TUPLE is not None: anchors = [_ANCHOR_TUPLE] msg = { 'command': 'emit', 'anchors': [a.id for a in anchors], } if stream is not None: msg['stream'] = stream if direct_task is not None: msg['task'] = direct_task lines = [] for tup in tuples: msg['tuple'] = tup lines.append(json.dumps(msg)) lines.append('end') print('\n'.join(lines), file=_stdout)
def submit(self, metrics): # HACK - Copy and pasted from dogapi, because it's a bit of a pain to distribute python # dependencies with the agent. body = json.dumps({"series" : metrics}) headers = {'Content-Type':'application/json'} method = 'POST' params = {} if self.api_key: params['api_key'] = self.api_key url = '/api/v1/series?%s' % urlencode(params) start_time = time() status = None conn = self.http_conn_cls(self.api_host) try: conn.request(method, url, body, headers) #FIXME: add timeout handling code here response = conn.getresponse() status = response.status response.close() finally: conn.close() duration = round((time() - start_time) * 1000.0, 4) log.debug("%s %s %s%s (%sms)" % ( status, method, self.api_host, url, duration)) return duration
async def update_dbots(self): if not discord_bots_token: self.logger.warning( 'Tried to contact Discord Bots, but no token set!') return False data = dict(guild_count=len(self.bot.guilds)) dest = 'https://bots.discord.pw/api/bots/' + str( self.bot.user.id) + '/stats' headers = { 'Authorization': discord_bots_token, 'Content-Type': 'application/json' } with async_timeout.timeout(6): async with self.bot.cog_http.post(dest, data=json.dumps(data), headers=headers) as r: resp_key = f'(got {r.status} {r.reason})' if r.status == 200: self.logger.info( 'Successfully sent Discord Bots our guild count (got 200 OK)' ) else: self.logger.warning( 'Failed sending our guild count to Discord Bots! ' + resp_key)
def _postAgentInfoToServer(self): import json import time import uuid import requests import platform, socket if not self._send_controler % 12: self._send_controler += 1 return self._send_controler += 1 hostname = get_hostname(self._agentConfig) if self._agentConfig['tags']: tags = self._agentConfig['tags'].split(',') else: tags = [] hostid = uuid.uuid5(uuid.NAMESPACE_DNS, platform.node() + str(uuid.getnode())).hex url = self._agentConfig['m_url'].replace( 'api/v2/gateway/dd-agent', 'api/v2/agent/info/intake?api_key=') + self._agentConfig['api_key'] modified = time.mktime(time.localtime(time.time())) path = '' if platform.system() == 'Linux': path = _unix_confd_path() elif platform.system() == 'Windows' and os.path.exists( 'C:/ProgramData/Datamonitor/conf.d'): path = _windows_confd_path() elif platform.system() == 'Windows' and os.path.exists( 'C:/Documents and Settings/All Users/Application Data/Datamonitor' ): path = 'C:/Documents and Settings/All Users/Application Data/Datamonitor' if path: file_list = os.walk(path).next()[2] apps = [ f for f in file_list if f.endswith(".yaml") or f.endswith(".yaml.default") ] post_data = { "id": str(hostid), "hostname": hostname, "ip": self.ip, "tags": tags, "apps": apps, "source": "agent", "modified": int(modified), } try: headers = {"content-type": "application/json"} r = requests.post(url=url, data=json.dumps(post_data), headers=headers, verify=False) log.debug("http return code: %s" % str(r.status_code)) except Exception, e: log.error( "Uncaught exception on self._postAgentInfoToServer: %s" % e)
async def commit(self): """Commit the current datastore to disk.""" if self.backend == 'json': with open(self.path, 'w+') as storefile: storefile.write(json.dumps(self.store)) elif self.backend == 'bson': with open(self.path, 'wb+') as storefile: storefile.write(bson.dumps(self.store))
def _postMetrics(self): if len(self._metrics) > 0: self._metrics["uuid"] = get_uuid() self._metrics["internalHostname"] = get_hostname(self._agentConfig) self._metrics["apiKey"] = self._agentConfig["api_key"] MetricTransaction(json.dumps(self._metrics), headers={"Content-Type": "application/json"}) self._metrics = {}
def _postMetrics(self): if len(self._metrics) > 0: self._metrics['uuid'] = get_uuid() self._metrics['internalHostname'] = get_hostname(self._agentConfig) self._metrics['apiKey'] = self._agentConfig['api_key'] MetricTransaction(json.dumps(self._metrics), headers={'Content-Type': 'application/json'}) self._metrics = {}
def serialize_metrics(metrics): serialized = json.dumps({"series" : metrics}) if len(serialized) > COMPRESS_THRESHOLD: headers = {'Content-Type': 'application/json', 'Content-Encoding': 'deflate'} serialized = zlib.compress(serialized) else: headers = {'Content-Type': 'application/json'} return serialized, headers
def renderPackets(self, packets=None): if packets is None: packets = self.buffer if self.permVars['se']: sseid = "\r\n" else: sseid = "" if self.permVars["se"] and packets: sseid = "id: %s\r\n\r\n"%(packets[-1][0],) return "%s(%s)%s%s"%(self.permVars["bp"], json.dumps(packets), self.permVars["bs"], sseid)
def serialize_metrics(metrics): serialized = json.dumps({"series": metrics}) if len(serialized) > COMPRESS_THRESHOLD: headers = { 'Content-Type': 'application/json', 'Content-Encoding': 'deflate' } serialized = zlib.compress(serialized) else: headers = {'Content-Type': 'application/json'} return serialized, headers
def commit(self): """Commit the current datastore to disk.""" atompath = self.path + '.atom' + str(random.randint(300, 4000)) if self.backend == 'json': with open(atompath, 'w+') as storefile: storefile.write(json.dumps(self.store)) shutil.move() elif self.backend == 'bson': with open(atompath, 'wb+') as storefile: storefile.write(bson.dumps(self.store)) shutil.move(atompath, self.path)
def members(): all_users = user_server.get_list(limit=-1) users = [] for user in all_users: if user.is_student: users.append({ 'name': user.name, 'college': SCHOOL_COLLEGE_MAP[user.college] if user.college else '', 'grade': user.grade + u'级' if user.grade else '', 'situation': user.situation }) return json.dumps({ 'data': users })
def recent_contests(): import json json_file = open(RECENT_CONTEST_JSON, 'r').read() json_contests = json.JSONDecoder().decode(json_file) contests = [] for contest in json_contests: name, link = contest['name'], contest['link'] new_contest = { 'oj': contest['oj'], 'name': '<a href="' + link + '" class="contest-name" title="' + name + '">' + name + '</a>', 'start_time': contest['start_time'], 'access': contest['access'], } contests.append(new_contest) return json.dumps({ 'data': contests })
def request_params(self, url, method, **kwargs): """ Constructs the parameters for a http request """ params = dict() if self._access_key is not None: params['access_key'] = self._access_key if kwargs: params.update(kwargs) if method == "GET": url = url + "?" + urllib.urlencode(params, doseq=True) body = None else: body = json.dumps(params, cls=EasyPeasyJsonEncoder) return url, method, body, _def_headers
def http_emitter(message, log, agentConfig): "Send payload" log.debug('http_emitter: attempting postback to ' + agentConfig['dd_url']) # Post back the data payload = json.dumps(message) zipped = zlib.compress(payload) log.debug( "payload_size=%d, compressed_size=%d, compression_ratio=%.3f" % (len(payload), len(zipped), float(len(payload)) / float(len(zipped)))) # Build the request handler apiKey = message.get('apiKey', None) if not apiKey: raise Exception("The http emitter requires an api key") url = "%s/intake?api_key=%s" % (agentConfig['dd_url'], apiKey) headers = post_headers(agentConfig, zipped) proxy_settings = agentConfig.get('proxy_settings', None) urllib2 = get_http_library(proxy_settings, agentConfig['use_forwarder']) try: request = urllib2.Request(url, zipped, headers) # Do the request, log any errors opener = get_opener(log, proxy_settings, agentConfig['use_forwarder'], urllib2) if opener is not None: urllib2.install_opener(opener) response = urllib2.urlopen(request) try: log.debug('http_emitter: postback response: ' + str(response.read())) finally: response.close() except urllib2.HTTPError, e: if e.code == 202: log.debug("http payload accepted") else: raise
async def update_discordlist(self): if not discordlist_token: self.logger.warning( 'Tried to contact DiscordList, but no token set!') return False data = {'token': discordlist_token, 'guilds': len(self.bot.guilds)} dest = 'https://bots.discordlist.net/api' headers = {'Content-Type': 'application/json'} with async_timeout.timeout(6): async with self.bot.cog_http.post(dest, data=json.dumps(data), headers=headers) as r: resp_key = f'(got {r.status} {r.reason})' if r.status == 200: self.logger.info( 'Successfully sent DiscordList our guild count! (got 200 OK)' ) else: self.logger.warning( 'Failed sending our guild count to DiscordList! ' + resp_key)
def submit(self, metrics): # HACK - Copy and pasted from dogapi, because it's a bit of a pain to distribute python # dependencies with the agent. conn = self.http_conn_cls(self.api_host) body = json.dumps({"series": metrics}) headers = {"Content-Type": "application/json"} method = "POST" params = {} if self.api_key: params["api_key"] = self.api_key url = "/api/v1/series?%s" % urlencode(params) start_time = time.time() conn.request(method, url, body, headers) # FIXME: add timeout handling code here response = conn.getresponse() duration = round((time.time() - start_time) * 1000.0, 4) logger.info("%s %s %s%s (%sms)" % (response.status, method, self.api_host, url, duration))
def update(self, value): """Update the config Parameters: value The config value Returns: Nothing """ if not isinstance(value, dict): self.logger.error("[%s] Failed to update config, value must be a dict", self.Type) return False # Validate try: self.validate(value) except: self.logger.exception("[%s] Failed to validate config value: [%s]", self.Type, json.dumps(value, ensure_ascii = False)) return False # Remove all values from self and update new values def updateConfig(): """Update the config """ self.clear() super(ConfigSection, self).update(value) self._timestamp = time.time() # Update if self._reloadLock: with self._reloadLock: updateConfig() else: updateConfig() # If reload is required if self.ReloadRequired: self._reloadEvent.set() # Updated self._updatedEvent.set() if self.logger.isEnabledFor(logging.DEBUG): self.logger.debug("[%s] Config updated [%s]", self.Type, json.dumps(value, ensure_ascii = False)) # Done return True
def http_emitter(message, log, agentConfig): "Send payload" log.debug('http_emitter: attempting postback to ' + agentConfig['dd_url']) # Post back the data payload = json.dumps(message) zipped = zlib.compress(payload) log.debug("payload_size=%d, compressed_size=%d, compression_ratio=%.3f" % (len(payload), len(zipped), float(len(payload))/float(len(zipped)))) # Build the request handler apiKey = message.get('apiKey', None) if not apiKey: raise Exception("The http emitter requires an api key") url = "%s/intake?api_key=%s" % (agentConfig['dd_url'], apiKey) headers = post_headers(agentConfig, zipped) proxy_settings = agentConfig.get('proxy_settings', None) urllib2 = get_http_library(proxy_settings, agentConfig['use_forwarder']) try: request = urllib2.Request(url, zipped, headers) # Do the request, log any errors opener = get_opener(log, proxy_settings, agentConfig['use_forwarder'], urllib2) if opener is not None: urllib2.install_opener(opener) response = urllib2.urlopen(request) try: log.debug('http_emitter: postback response: ' + str(response.read())) finally: response.close() except urllib2.HTTPError, e: if e.code == 202: log.debug("http payload accepted") else: raise
def submit(self, metrics): # HACK - Copy and pasted from dogapi, because it's a bit of a pain to distribute python # dependencies with the agent. conn = self.http_conn_cls(self.api_host) body = json.dumps({"series": metrics}) headers = {'Content-Type': 'application/json'} method = 'POST' params = {} if self.api_key: params['api_key'] = self.api_key url = '/api/v1/series?%s' % urlencode(params) start_time = time() conn.request(method, url, body, headers) #FIXME: add timeout handling code here response = conn.getresponse() duration = round((time() - start_time) * 1000.0, 4) logger.info("%s %s %s%s (%sms)" % (response.status, method, self.api_host, url, duration))
def submit_events(self, events): headers = {'Content-Type':'application/json'} method = 'POST' events_len = len(events) event_chunk_size = self.event_chunk_size for chunk in chunks(events, event_chunk_size): payload = { 'apiKey': self.api_key, 'events': { 'api': chunk }, 'uuid': get_uuid(), 'internalHostname': get_hostname() } params = {} if self.api_key: params['api_key'] = self.api_key url = '/intake?%s' % urlencode(params) status = None conn = self.http_conn_cls(self.api_host) try: start_time = time() conn.request(method, url, json.dumps(payload), headers) response = conn.getresponse() status = response.status response.close() duration = round((time() - start_time) * 1000.0, 4) log.debug("%s %s %s%s (%sms)" % ( status, method, self.api_host, url, duration)) finally: conn.close()
def submit_events(self, events): headers = {'Content-Type': 'application/json'} method = 'POST' events_len = len(events) event_chunk_size = self.event_chunk_size for chunk in chunks(events, event_chunk_size): payload = { 'apiKey': self.api_key, 'events': { 'api': chunk }, 'uuid': get_uuid(), 'internalHostname': get_hostname() } params = {} if self.api_key: params['api_key'] = self.api_key url = '/intake?%s' % urlencode(params) status = None conn = self.http_conn_cls(self.api_host) try: start_time = time() conn.request(method, url, json.dumps(payload), headers) response = conn.getresponse() status = response.status response.close() duration = round((time() - start_time) * 1000.0, 4) log.debug("%s %s %s%s (%sms)" % (status, method, self.api_host, url, duration)) finally: conn.close()
def serialize_metrics(metrics): return json.dumps({"series" : metrics})
def main_rank_table(): main_rank_list = general.get_rank_list() return json.dumps({ 'data': main_rank_list })
def renderRequest(self, data, request): request.setHeader('Content-type', self.permVars['ct']) x = self.tryCompress("%s(%s)%s"%(self.permVars["rp"], json.dumps(data), self.permVars["rs"]), request) return x
def running_record(self): """ 构造一次跑步记录 """ points_per_loop = json_load(self.A_Loop_GPS_JSON) # 一圈的坐标 distance = config.getfloat("PB", "distance") # 总距离 km pace = config.getfloat("PB", "pace") # 速度 min/km stride_frequncy = config.getint("PB", "stride_frequncy") # 步频 步/min duration = distance * pace * 60 # 用时 s cal_per_loop = lambda: 20 + random.random() * (23 - 20) # 20-23 每圈 point_delta = lambda: (random.random() - 0.5) * 0.00005 # 随机坐标偏差 distance_delta_rate = lambda: 1 + (random.random() - 0.5 ) * 0.1 # 0.95 - 1.05 的距离随机倍率 stride_frequncy_delta = lambda: int( (random.random() - 0.5) * 2 * 10) # 10步/min 的随机步频偏差 random_alt = lambda: round(42 + random.random() * (48 - 42), 1) # 42-48 海拔 random_speed = lambda: round(3.1 + random.random() * (4.4 - 3.1), 2) # 没搞懂 speed 怎么定义的 ... def locus_generator(): end_time = int(time.time() * 1000) start_time = now_time = end_time - int(duration * 1000) # 从开始时间起 now_stepcount = 0 now_distance = 0.00 while now_time <= end_time: for point in points_per_loop: per_distance = 0.4 / len( points_per_loop) * distance_delta_rate() # 两点间距离 km now_stepcount += int( (stride_frequncy + stride_frequncy_delta()) * per_distance * pace) now_distance += per_distance yield { # "id": ??? # 拿不到主键,但是不加主键也可以提交成功,数据库应该设置了主键自增长 "alt": random_alt(), "speed": random_speed(), "heartrate": 0, "distance": now_distance, "lat": round(point['lat'] + point_delta(), 8), "lng": round(point['lng'] + point_delta(), 8), "stepcount": now_stepcount, "traintime": now_time, } now_time += int(per_distance * pace * 60 * 1000) # 时间间隔 ms locuslist = list(locus_generator()) distance = locuslist[-1]['distance'] # 实际的距离 km duration = (locuslist[-1]['traintime'] - locuslist[0]['traintime']) / 1000 # 实际的用时 s return json.dumps({ "biggerId": self.biggerId, "token": self.token, "locusrlist": [{ "cal": int(cal_per_loop() * distance * 1000 / 400), "distance": round(distance, 2), "duration": int(duration), "heartrate": 0, "team": 1, # "pace": ??? # 此处的 pace 与跑步记录中的 pace 含义不统一 # "intermittent": ??? "locuslist": [locuslist], }], }).encode('utf-8')
def tojson(self): """Convert the object to it's json representation.""" return json.dumps(self.jsonable())
def format_body(message): payload = json.dumps(message) return zlib.compress(payload)
def write_json(self, data, status_code=200, msg='success.'): self.finish(dumps({ 'code': status_code, 'msg': msg, 'data': data }))
config = Config() logger = Logger("runner") parser = OptionParser(usage="PKU running helper ! Check your config first, then enjoy yourself !") parser.add_option("-c", "--check", help="show 'config.ini' file", action="store_false") parser.add_option("-s", "--start", help="run the runner's client", action="store_false") options, args = parser.parse_args() if options.check is not None: for section in config.sections(): print("Section [%s]" % section) print(json.dumps(dict(config[section]), indent=4)) print("\n") elif options.start is not None: app = config.get('Base', 'APP') if app == 'PB': from PB import PBClient as Client elif app == "PKURunner": from PKURunner import PKURunnerClient as Client elif app == "Joyrun": from Joyrun import JoyrunClient as Client else: raise ValueError("unsupported running APP -- %s !" % app)
def fitch_status(oj_name): headers = ['account_name', 'run_id', 'pro_id', 'lang', 'run_time', 'memory', 'submit_time'] ret = status_server.DataTablesServer(request.form, oj_name, headers).run_query() return json.dumps(ret, cls=CJsonEncoder)
def serialize_event(event): return json.dumps(event)
def format_body(message, logger): payload = json.dumps(message) payloadHash = md5(payload).hexdigest() postBackData = urllib.urlencode({'payload' : payload, 'hash' : payloadHash}) return postBackData
def write_json(self, data, status_code=200, msg='success.'): self.finish(dumps({'code': status_code, 'msg': msg, 'data': data}))
def serialize_metrics(metrics): return json.dumps({"series": metrics})
from db import user from db.login_info import get_by_id from db.models import LoginInfo from db.user import save from util import json save(LoginInfo(username='******',password='******',enable=0)) rts = json.dumps(get_by_id(1)) # 加上ensure_ascii=False 是解决中文乱码问题 print(rts) rts = json.dumps(user.get_by_id(1)) print(rts)