def get_top_words(request, n): url = request # так привычнее try: r = requests.get(url) r.encoding = 'utf-8' except MissingSchema: print('Bad url - no schema') return HttpResponse(status_code=404, data=json.dumps({'Failed': 'bad request'})) except requests.exceptions.InvalidURL: print('Bad url') return HttpResponse(status_code=404, data=json.dumps({'Failed': 'bad request'})) except requests.exceptions.ConnectionError: print('Bad url') return HttpResponse(status_code=404, data=json.dumps({'Failed': 'bad request'})) soup = BeautifulSoup(r.text, 'html.parser') lst_of_words = soup.get_text().split() lst_cleared_1_stage = list( map(lambda x: x.strip('()[]/.,!?<>=;:').lower(), lst_of_words)) lst_cleared_2_stage = list( filter(lambda x: len(x) > 1 and x not in stop_words, lst_cleared_1_stage)) dict_of_popular_words = dict(Counter(lst_cleared_2_stage).most_common(n)) response = json.dumps(dict_of_popular_words, ensure_ascii=False) return HttpResponse(data=response)
def send_response(url, func=func_server, conn=conn, lock=lock): try: response = func(url) data_response = response.data status_code = response.status_code if data_response: lock.acquire() global count_url count_url += 1 conn.sendall(data_response) # self.conn.sendall(JsonResponse(data={'message': self.message}).data) lock.release() else: status_code = 404 conn.sendall( HttpResponse(status_code=404, data={ 'error': '404', 'message': 'not found' }).data) logging.error(f"404 {url}") except Exception as e: status_code = 404 conn.sendall( HttpResponse(status_code=404, data={ 'error': '404', 'message': 'not found' }).data) logging.error(f"404 {url} {e}") print(f"Get data from <{url}> status code {status_code}\n")
def get_user_by_id(user_id): try: user = session.query(User).filter_by(id=int(user_id)).one() except: return HttpResponse("USER_NOT_FOUND", 404) return HttpResponse(UserDto().dump(user), 200)
def create_user(user_dto): try: if user_dto['password'] ==None: return HttpResponse("INVALID_INPUT", 405) user = User(**user_dto) except: return HttpResponse("INVALID_INPUT", 405) session.add(user) session.commit() return HttpResponse("USER_CREATED", 201)
def run(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind((self.address, self.port)) sock.listen(5) connection, address = sock.accept() try: connection.settimeout(5) buf = connection.recv(1024) req = HttpRequest(buf) res = HttpResponse() if self._resource_exists(req.target) != False: target = self._resource_exists(req.target) if DEBUG: print target with open(target, 'r') as f: res.setBody(f.read()) res.setStatus(200) else: res.setStatus(404) connection.send(res.__str__()) if DEBUG: print req print res except socket.timeout: print 'timeout' finally: connection.close() sock.close()
def update_wallet(wallet_id, wallet_dto): try: wallet = session.query(Wallet).filter_by(id=int(wallet_id)).one() except: return HttpResponse("WALLET_NOT_FOUND", 404) try: if wallet_dto.get('balance', -100): update_amount = int(wallet_dto['balance']) if update_amount <= 0: return HttpResponse("BAD_AMOUNT", 405) else: wallet.balance += int(wallet_dto['balance']) if wallet_dto.get('is_default', None): wallet.is_default = wallet_dto['is_default'] except: return HttpResponse("INVALID_INPUT", 405) session.commit() return HttpResponse("WALLET_UPDATED", 200)
def update_user(user_id, data): try: user = session.query(User).filter_by(id=int(user_id)).one() except: return HttpResponse("NOT_FOUND", 404) try: if data.get('first_name', None): user.first_name = data['first_name'] if data.get('last_name', None): user.last_name = data['last_name'] if data.get('user_name', None): user.user_name = data['user_name'] if data.get('password', None): user.password = data['password'] except: return HttpResponse("INVALID_INPUT", 405) session.commit() return HttpResponse("USER_UPDATED", 200)
def start(): socket_in = _create_socket() while True: client_socket, client_address = socket_in.accept() print(f"Established connection with client: {client_address}") print("Waiting for message from client...") raw_request = client_socket.recv(REQ_BUFFER_SIZE) print(f"Message received. Attempting to parse...") try: request = HttpRequest(raw_request) response = HttpResponse.ok(request) except InvalidHttpRequestException as e: response = HttpResponse.bad_request(e) client_socket.send(response.serialize()) client_socket.close() print("Socket closed.")
def get_default_wallet(attribute, transaction_dto): try: print(int(transaction_dto[attribute])) wallet = session.query(Wallet) \ .filter_by(owner_id=int(transaction_dto[attribute])) \ .filter_by(is_default=True) \ .one() return wallet except: formatted = attribute.split('_') message = f'{formatted[0]} wallet was not found' return HttpResponse(message, 404)
def read_from_client(self, ed, connections, requests, responses, fileno): # handle input data try: data = connections[fileno].recv(1024) req = requests[fileno] req.add_data(data) if req.is_ready: ed.modify(fileno, select.EPOLLOUT) responses[fileno] = HttpResponse(req, self.rtdir, self.srv_name) except Exception as ex: logging.exception('erorror read:{}'.format(ex))
class HttpHandler(BaseHandler): ''' Main Http Handler Class which will be extended by request handlers. ''' def __call__(self,request,*args,**kwargs): """ Works when controller executes mapped subclass """ #super(BaseHandler,self).__call__(request) self._template = None self.response = HttpResponse() self.request = request self.report = Report() self.cookies = CookieHandler(self) self.init(*args,**kwargs) """ Subclasses will have get and post methods to handle requests. This method executes these methods by "method" attribute of request object. """ getattr(self,'post' if self.request.method=='POST' else 'get')(*args,**kwargs) if self.template: self.response.write(self.template.render()) return self.response def init(self,*args): pass def get(self,*args,**kwargs): pass def post(self,*args,**kwargs): self.get(*args,**kwargs) def redirect(self,url): self.response.status = '307 Temporary Redirect' self.response.headers['Location'] = url @property def template(self): return self._template @template.setter def template(self,template): self._template = template
def create_wallet(wallet_dto): try: user = session.query(User).filter_by( id=int(wallet_dto['user_id'])).one() except: return HttpResponse("NOT_FOUND", 404) wallets = session.query(Wallet).filter_by( owner_id=int(wallet_dto['user_id'])).all() if len(wallets) >= 1: wallet = Wallet(balance=0, owner_id=wallet_dto['user_id'], is_default=False) else: wallet = Wallet(balance=0, owner_id=wallet_dto['user_id'], is_default=True) session.add(wallet) session.commit() return HttpResponse("SUCCESSFULLY_CREATED_WALLET", 201)
def create_transaction(transaction_dto): if transaction_dto.get('amount') is not None: amount = int(transaction_dto['amount']) if amount <= 0: return HttpResponse("INVALID_AMOUNT", 405) else: return HttpResponse("AMOUNT_NOT_PRESENT", 405) sender_wallet = get_default_wallet('sender_id', transaction_dto) receiver_wallet = get_default_wallet('receiver_id', transaction_dto) if not isinstance(sender_wallet, Wallet) or not isinstance(receiver_wallet, Wallet): return receiver_wallet if sender_wallet.balance < amount: return HttpResponse("INSUFFICIENT_FUNDS", 405) transaction = Transaction(sender_id=int(transaction_dto['sender_id']), receiver_id=int(transaction_dto['receiver_id']), amount=amount, time_stamp=datetime.today()) sender_wallet.balance = sender_wallet.balance - amount receiver_wallet.balance += amount session.add(transaction) session.commit() return HttpResponse(TransactionDto().dump(transaction), 201)
def run(self): while True: content = self.q.get() try: response = self.func(content['data']) data_response = response.data status_code = response.status_code if data_response: self.lock.acquire() global count_url count_url += 1 self.conn.sendall(data_response) # self.conn.sendall(JsonResponse(data={'message': self.message}).data) self.lock.release() else: status_code = 404 self.conn.sendall(HttpResponse(status_code=404, data={'error': '404', 'message': 'not found'}).data) logging.error(f"404 {content['data']}") except Exception as e: status_code = 404 self.conn.sendall(HttpResponse(status_code=404, data={'error': '404', 'message': 'not found'}).data) logging.error(f"404 {content['data']} {e}") print(f"{status_code}\n")
def process (self, http_request) -> HttpResponse: env = http_request.get_cgi_config() env.update({**self.__config, 'wsgi.input': io.StringIO(http_request.get_body())}) headers = [] def start_response (status, response_headers, exc_info = None): nonlocal headers headers = [status, response_headers] result = self.__application(env, start_response) status, response_headers = headers return HttpResponse(status, response_headers, result, env)
def __call__(self,request,*args,**kwargs): """ Works when controller executes mapped subclass """ #super(BaseHandler,self).__call__(request) self._template = None self.response = HttpResponse() self.request = request self.report = Report() self.cookies = CookieHandler(self) self.init(*args,**kwargs) """ Subclasses will have get and post methods to handle requests. This method executes these methods by "method" attribute of request object. """ getattr(self,'post' if self.request.method=='POST' else 'get')(*args,**kwargs) if self.template: self.response.write(self.template.render()) return self.response
def get_user_wallets(user_id): try: wallets = session.query(Wallet).filter_by(owner_id=int(user_id)).all() except: return HttpResponse("NO_WALLETS", 404) return HttpResponse(wallets_dto.dump(wallets), 200)