Hina Ch4n Hina Ch4n - 21 days ago 5
Python Question

Error while checking request.method

When I try to do a if statement like this:

if request.method == "POST":


I get this error:

AttributeError: 'module' object has no attribute 'method'


Full traceback:

Traceback (most recent call last):
File "C:\Python34\lib\site-packages\flask\app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python34\lib\site-packages\flask\app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python34\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Python34\lib\site-packages\flask\app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask\app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python34\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python34\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Python34\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python34\lib\site-packages\flask\app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "K:\HinaCh4n-PenSuite\main.py", line 75, in login_page
raise e
File "K:\HinaCh4n-PenSuite\main.py", line 55, in login_page
if request.method == "POST":
AttributeError: 'module' object has no attribute 'method'


Here are my imports:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Flask, request, render_template, flash, url_for, redirect, session
from flask_cors import CORS, cross_origin
from dbconnect import connection
from pymysql import escape_string as thwart
from passlib.hash import sha256_crypt
from functools import wraps
import random
from base64 import b64encode
from wtforms import Form, BooleanField, TextField, PasswordField, validators
import gc
from urllib import request


And my function:

@app.route('/login', methods=['POST', 'GET'])
def login_page():
error = " "
try:
c, conn = connection()
if request.method == "POST":

data = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(request.form['username'])))
print(data)
if data < 1 :
return "0"
else:
data = c.fetchone()[2]
if sha256_crypt.verify(request.form['password'], data):
session['logged_in'] = True
session['username'] = request.form['username']
return "1"
else:
return "0"

gc.collect()

return render_template("login.html")

except Exception as e:
raise e
error = "Invalid credentials"
return "error"

Answer

Because you're importing 2 separate requests (from flask and urllib), it's not working properly. Change your import for urllib's request to this, and update all references for urllib's request from request to req

from urllib import request as req