def keepAvahiBrowsing(thread_name, ipaddresses, lock, option): print("start keepAvahiBrowsing: threadname=" + thread_name + " option=" + option) while( True ): (output, err) = runShellCommand("avahi-browse -rtp _coap._udp | grep 'piSenseHat-4'") if( (err == None) or (output == None or len(output) == 0) ): print("SpotFinderThread: error avahi-browse err=" + str(err) + ",out=" + output) else: lines = output.splitlines() for line in lines: arr = line.split(";") if( len(arr) >= 8 ): ip = arr[7] #print("SpotFinderThread: testing ip=" + ip + ",isValidIp=" + str(isValidIpv4(ip)) + ",in=" + str(ip in ipaddresses)) lock.acquire() if( isValidIpv4(ip) and (ip in ipaddresses) == False ): print("SpotFinderThread: client-ip found ip=" + ip) b = False if(option == "skip"): b = True else: b = sendServerIPtoParkingSpot("sendServerIPtoParkingSpot", ip, getOwnIP()) if( b == True ): RequestUtils.createParkingSpot(ip) ipaddresses.append(ip) lock.release() time.sleep(5)
def init(p_ownIP, option): global re_ip global ownIP global ipaddresses ownIP = p_ownIP ipaddresses = [] re_ip = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') RequestUtils.clearDB() thread.start_new_thread(keepAvahiBrowsing, ("SpotFinderThread,keepAvahiBrowsing", ipaddresses, lock, option)) thread.start_new_thread(keepReadingJoystickFiles, ("SpotFinderThread,keepReadingJoystickFiles", ipaddresses, lock))
def validate_service(current_item): """ Validate a single service :param current_item: Input item/service :return: Dictionary of results """ item_id = current_item[0] item_content = current_item[1] item = item_content['agolItem'] type_keywords = item['typeKeywords'] require_token = _requires_token("Requires Subscription", type_keywords) print(f"{item_id}\t{item_content['title']}") print(f"default retry count threshold: {item_content['default_retry_count']}") print(f"default timeout threshold: {item_content['default_timeout']}") print(f"service url: {item_content['service_url']}") print(f"requires token: {require_token}") response = RequestUtils.check_request(path=item_content["service_url"], params={}, try_json=True, add_token=require_token, retry_factor=item_content["default_retry_count"], timeout_factor=item_content["default_timeout"], token=item_content["token"], id=item_id) print("\n") return current_item[0], { **current_item[1], **{"serviceResponse": response} }
def getCvImageAndUrl(self, index, useThumbnail=False): if index >= self._numResultsReceived: return None, None result = self._results[index] if useThumbnail: url = result.thumbnail_url else: url = result.content_url return RequestUtils.cvImageFromUrl(url), url
def keepReadingJoystickFiles(thread_name, ipaddresses, lock): # reads the joystickfiles jsUpdate-<end_point>.txt that are output by Java # assumes these are in the directory below print("start keepReadingJoystickFiles thread_name=" + thread_name); path = os.path.realpath(__file__) path = path[0:path.rindex("/")] path = path[0:path.rindex("/")+1] # get parent directory prefix = "jsUpdate-" suffix = ".txt" print("keepReadingJoystickFiles path=" + path) while( True ): fileNames = os.listdir(path) for fileName in fileNames: if prefix in fileName: endpoint = fileName[len(prefix):len(fileName)-len(suffix)] print("keepReadingJoystickFiles fileName=" + fileName, "endpoint=" + endpoint) RequestUtils.enterOrLeaveVehicle(endpoint) # delete file os.remove(fileName) time.sleep(1)
def handle_mobile_request(self, request): logging.info("Mobile request -> " + request) parser = RequestUtils.validate(request) if len(parser) == 3: try : request_code = int(parser[0]) self.send_mob_response(request_code, parser[1], parser[2]) except : self.handle_exception() self.send_resp("640 <Invalid request format>##" \ + parser[2] + "##") else : self.send_resp("640 <Invalid request format>####")
def testValidator(self): self.assertEqual(RequestUtils.validate( \ ""), []) self.assertEqual(RequestUtils.validate( \ "<>####"), []) self.assertEqual(RequestUtils.validate( \ "600 <>####"), []) self.assertEqual(RequestUtils.validate( \ "600 <>##ROUTER=OPX##"), ['600','','ROUTER=OPX']) self.assertEqual(RequestUtils.validate( \ "800 app_id=?>##ROUTER=OPX##"), []) self.assertEqual(RequestUtils.validate( \ "800 <app_id=?##ROUTER=OPX##"), []) self.assertEqual(RequestUtils.validate( \ "800 <app_id=?>ROUTER=OPX##"), []) self.assertEqual(RequestUtils.validate( \ "800 <app_id=?>##ROUTER=OPX"), []) self.assertEqual(RequestUtils.validate( \ "800 <app_id=?>##ROUTER=OPX##"), \ ['800','app_id=?','ROUTER=OPX'])
def create_response(self, response, request_echo_params): parser = RequestUtils.validate(response) #merging the echo_params response = parser[0] + " <" + parser[1] + ">" response += "##" + request_echo_params + "##"; response = response.replace("<<","<").replace(">>",">"); if len(parser) == 3 and parser[2] != '': response += "," + parser[2] parsed_echo_params = Parser.parse_echo_params(parser[2]) if parsed_echo_params.has_key('TEST_TIMEOUT'): time_out = int(parsed_echo_params['TEST_TIMEOUT']) time.sleep(time_out) response += "##" return response
def check_layer_url(layer=None) -> dict: """ Check that the Item's url is valid """ if layer is None: layer = {} response = { "id": layer["id"], "success": False } if layer["success"]: url = layer["url"] response = RequestUtils.check_request(path=url, params=layer['params'], try_json=layer['try_json'], add_token=layer['add_token'], retry_factor=layer['retryCount'], timeout_factor=layer['timeout'], id=layer["id"], token=layer['token']) return response
def check_alfp_url(input_data=None) -> dict: """ :param input_data: :return: """ if input_data is None: input_data = {} response = RequestUtils.check_request(path=input_data["url"], params=input_data['params'], try_json=input_data['try_json'], add_token=input_data['add_token'], retry_factor=input_data['retry_factor'], timeout_factor=input_data['timeout_factor'], token=input_data['token'], id=input_data["id"]) return { "id": input_data["id"], "response": response }
def create_802_response(self, params, echo_params): params = Parser.parse_800_params(params) if len(params) == 0: return ["", "640 <Invalid Request>"] echo_params = Parser.parse_echo_params(echo_params) if not echo_params.has_key('ROUTER'): return ["", "640 <No Router>"] app_id = "" if params.has_key('APP_ID'): app_id = params['APP_ID'] if app_id == '?' or not devices.has_key(app_id): app_id = RequestUtils.generateAppId() self.addDeviceData(app_id, params, echo_params) response = "802 <APP_ID=" + app_id + ">" else : response = "640 <No app_id in the request>" response = response.replace("####", "##"); return [app_id, response]
parsed['postAdForm.mapRadius'] = ['75.29440284317512'] parsed['postAdForm.adType'] = ['OFFER'] parsed['postAdForm.priceType'] = ['FIXED'] parsed['postAdForm.priceAmount'] = ['1000'] parsed['postAdForm.attributeMap[forsaleby_s]'] = ['ownr'] parsed['postAdForm.title'] = ['asdfasdff'] parsed['postAdForm.description'] = ['asdfasdfasdfasdf'] # parsed['file']=[''] # parsed['postAdForm.youtubeVideoURL']=[''] # parsed['location-fallback']=['k1m 0s6'] # parsed['postAdForm.phoneNumber']=[''] parsed['featuresForm.topAdDuration'] = ['7'] # parsed['submitType']=['saveAndCheckout'] session = login.new_session() sessionStr = RequestUtils.get_session_str(session) client = httpclient.AsyncHTTPClient() httpmethod = "POST" url = "https://www.kijiji.ca/p-submit-ad.html" nativeCookies = { 'Content-Type': 'application/x-www-form-urlencoded', "Cookie": sessionStr } necessary = [] toCheck = len(parsed.keys()) def handle_create(field, isNec): print("Failed field is :", field) necessary.append(field)
import PostAdAsync import RequestUtils import delete_ad from tornado import ioloop, httpclient # with open('./data/failed-creates.json', 'r') as f: # with open('./data/all-categories-flat.json', 'r') as flatFile: # sess = RequestUtils.get_session() # allData = json.load(flatFile) # data = json.load(f) # for d in data: # PostAdAsync.get_ad(d, sess) # ioloop.IOLoop.instance().start() # delete_ad.delete_all() # newFailed = copy.copy(PostAdAsync.failed) # PostAdAsync.failed = [] sessionStr = RequestUtils.get_session() failed = [12, 646, 772, 776, 773, 782, 760] with open('./data/failed-data-fields.json', 'r') as failedFieldFile: failedFields = json.load(failedFieldFile) for d in failed: PostAdAsync.get_ad(d, sessionStr, None) ioloop.IOLoop.instance().start() # with open('./data/all-categories-flat.json', 'r') as flatFile: # allData = json.load(flatFile) # for d in failed: # print(allData[str(d)])
request = httpclient.HTTPRequest(url, method=httpmethod, headers=nativeCookies) client.fetch( request, partial(handle_getad, categoryId, sessionStr, extraFields, finalCb)) def Create_Callbacks(): cbs = {} cbs['OnSuccessCreate'] = handle_success_create cbs['OnFailCreate'] = handle_failed_create return cbs def main(session, toTest): for c in toTest: get_ad(c, session, None, handle_delete) if __name__ == '__main__': catId = 10 children = KijijiCategories.load_child_flat(catId) session = login.new_session() cookiesStr = RequestUtils.get_session_str(session) main(cookiesStr, toTest) ioloop.IOLoop.instance().start() with open('./data/failed-creates.json', 'w') as f: dump(failed, f)
def get_beautiful_soup(url): print url request = RequestUtils.get_request(url) soup = BeautifulSoup(request.content.decode('utf-8', 'ignore'), 'lxml') return soup