def get_reponse(self): self.build_request_url() write_log(u"开始请求广告") gr = GetResponse() res = "" if gr: write_log(u"请求成功") try: res = gr.get_response(self.url) self.res = res.json() write_log(u"召回信息:\n{0}".format(json.dumps(self.res, indent=4))) except Exception: write_log(u'请求返回的不是json对象\n\t{0}'.format(res.content),'warn') else: write_log(u"请求失败!!!", "error")
def set_request_info(self, file_index): self.backend_request_data_path = self.backend_request_data_path.format( file_index) self.backend_response_data_path = self.backend_response_data_path.format( file_index) write_log("request data path: {0}".format( self.backend_request_data_path)) write_log("request host: {0}".format(self.backend_host)) write_log("request method: {0}".format(self.backend_method)) self.backend_url = self.backend_url.format(self.backend_host) write_log("request route: {0}".format(self.backend_url)) self.query_data = read_file(self.backend_request_data_type, "dict", self.backend_request_data_path)
def build_request_url(self): query_data = {} write_log(u"开始读取请求数据所在的文件:{0}".format(self.data_path + self.request_file_name)) request_params = read_csv_to_list(self.data_path + self.request_file_name) if not request_params: write_log(u"读取文件失败!!!", "error") else: write_log(u"读取请求数据成功") for param, value in request_params: if param.startswith('#'): continue query_data.setdefault(param, value) if query_data.get('http_req') == "2": write_log(u"本次请求是ios发出,需要用https来请求广告") self.url = self.url.replace("http", "https") query_url = urlencode(query_data) self.url = self.url.format(self.host, query_url)
def get_my_response(self, file_index): self.set_request_info(file_index) self.get_response(self.backend_url, self.backend_method, self.backend_headers, self.query_data) if self.res: if self.backend_response_data_type == "json": self.response = self.res.json() write_log("response is: {0}".format( json.dumps(self.response, indent=4))) else: self.response = self.res.content write_log("response is: {0}".format(self.response)) else: write_log("request failed, no response to check", 'error') raise ValueError
def send_tracking(self, tracking_data, set_domain=set()): # 只发送第一个campaign的tracking信息 self.find_all_urls(tracking_data) if not self.url_list: write_log('no url to send!', 'warn') return write_log('send url in response, summary: {0}'.format( len(self.url_list))) for url in self.url_list: try: for domain in set_domain: # 只发送指定domain的请求 if url.find(domain) in [7, 8]: self.get_response(url) except StandardError: write_log('send url failed \n %s!!!' % url, 'error')