def main(): """ start """ logger_config() client, channel = get_channel(credentials=CREDENTIALS) # В момент создания канала - сервер в подтверждение подключения запишет ваш логин в канал logging.INFO(channel.recv(BYTES)) # 'username' # channel.recv(n_bytes) - синтаксис получения данных из канала channel.send("some interactive string with bytes gi 0/1") # send() завершается быстрее, чем даные дойдут до сервера time.sleep(1) logging.Info(channel.recv(BYTES)) # Тут проблема channel.send("switch port to 5432") # send() завершается быстрее, чем даные дойдут до сервера time.sleep(1) logging.Info(channel.recv(BYTES)) # Тут проблема client.close()
def follow_back(api, config): followers = set(api.followers_ids(config.twitter_keys['screen_name'])) following = set(api.friends_ids(config.twitter_keys['screen_name'])) not_following_back = [item for item in followers if item not in following] for uid in not_following_back: user = api.get_user(uid) logging.Info("Following %d %s." % (uid, user.name)) api.create_friendship(uid, True) print("Finished. %d followed." % len(not_following_back))
def get(self, url, **kwargs): """ Helps to retrieve the entities Args: url(str):- API url which performs the get operation kwargs(dict) : - Info which is required to retrive the information from server. returns:- Response """ try: logging.Info(f"Making the get request with {url} with the provided data {kwargs}") request = requests.get(url=url) except HTTPError as http_err: logging.error(f'HTTP error occurred: {http_err}') except Exception as err: logging.error(f'Unexpected error occurred: {err}') pass
def post(self, url, **kwargs): """ Helps to send the entities to server Args: url(str):- API url which performs the post operation kwargs(dict) : - Info which needs to be sent to the server. returns:- Response """ headers = {'content-type': 'application/json'} try: logging.Info(f"Making the post request with {url} with the provided data {kwargs}") request = requests.post(url=url, headers=headers, data=kwargs) if request.text is not u'': return request.json() else: return request.text return request.response()
def __init__(self, name, salary): self.name = name self.salary = salary logging.Info("New object created")
logging.error(f'HTTP error occurred: {http_err}') except Exception as err: logging.error(f'Unexpected error occurred: {err}') def put(self, url, **kwargs): """ Helps to update the entities in server Args: url(str):- API url which performs the update operation kwargs(dict) : - Info which is required to update the entities. returns:- Response """ headers = {'content-type': 'application/json'} try: logging.Info(f"Making the put request with {url} with the provided data {kwargs}") request = requests.put(url=url) except HTTPError as http_err: logging.error(f'HTTP error occurred: {http_err}') except Exception as err: logging.error(f'Unexpected error occurred: {err}') def delete(self, url, **kwargs): """ Helps to update the entities in server Args: url(str):- API url which performs the delete operation kwargs(dict) : - Info which is required to delete the entities. returns:- Response """
def standardize_address(batch, usps_key): # Form voter addresses post_data = [] if batch is None: return [] for row in batch: try: raddnumber = row['RADDNUMBER'].strip() if row['RADDNUMBER'] else "" rstreetname = row['RSTREETNAME'].strip( ) if row['RSTREETNAME'] else "" if row['RAPARTMENT']: if ('APT' in row['RAPARTMENT']) or ( 'UNIT' in row['RAPARTMENT']) or ('PH' in row['RAPARTMENT']): rapartment = row['RAPARTMENT'] else: rapartment = "APT {}".format(row['RAPARTMENT']) else: rapartment = "" post_data.append({ 'address': u" ".join([raddnumber, rstreetname, rapartment]), 'city': row['RCITY'], 'state': 'NY' }) except Exception as e: logging.Info( "Could not form address in standardize_address, error: {}". format(e)) post_data.append(None) # Submit batch to API try: recv_data = address_information.verify(usps_key, *post_data) except Exception as e: logging.error( "Caught exception posting to standardize_address: {}".format(e)) # Match output = [] for i, row in enumerate(batch): if row is not None: out_dct = row.copy() else: continue # Try and use formatted address try: out_dct['voter_addr1'] = recv_data[i]['address'] if isinstance(recv_data[i]['zip5'], int): # So defensive recv_data[i]['zip5'] = "{:0.0f}".format(recv_data[i]['zip5']) out_dct['voter_addr2'] = "{}, {} {}".format( recv_data[i]['city'], recv_data[i]['state'], recv_data[i]['zip5']) except Exception as e: # Output from pyusps is Exception not dict, etc.; fall back on # constructed string try: out_dct['voter_addr1'] = post_data[i]['address'] except Exception as e: # e.g. because post_data[i] is None out_dct['voter_addr1'] = None out_dct['voter_addr2'] = "{}, NY {}".format( row['RCITY'], row['RZIP5']) # Form match string, whatever happens out_dct['match_string'] = "{} {}, {}, {}".format( out_dct['LASTNAME'].strip(), out_dct['FIRSTNAME'].strip(), out_dct['voter_addr1'], out_dct['voter_addr2']) output.append(out_dct) return output