Hack Forums
[Python] Function Returns None even though Return is stated - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Function Returns None even though Return is stated (/showthread.php?tid=5881639)



Function Returns None even though Return is stated - The.Businessman - Sep 5, 2018

Code
import requests

x = input("Gib email pls")

def CheckReg(page): #Checks if amazon account exists.
    try:
        with ss as requests.Session:
            ss.headers.update(
                {
                    'Host': 'www.amazon.com',
                    'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25',
                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Accept-Language': 'en-US,en;q=0.5',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'DNT': '1',
                    'Connection': 'keep-alive',
                    'Upgrade-Insecure-Requests': '1'
                })
            r = ss.get('https://www.amazon.com')
            cookies = r.cookies
            r = ss.post('https://www.amazon.com/ap/register?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dgno_newcust?&email={}'.format(page), cookies=cookies)
            if 'You indicated you are a new customer, but an account already exists with the e-mail' in r.text:
                return True
            elif 'Create your Amazon account' in r.text:
                return False
            else:
                return r.text
    except:
        pass

if __name__ == '__main__':
    y = CheckReg(str(x))
    print(y)

It returns "None"

Any idea what's the problem behind this ?


RE: Function Returns None even though Return is stated - Dr. Lecter - Sep 5, 2018

The ss variable is not defined correctly.


RE: Function Returns None even though Return is stated - The.Businessman - Sep 5, 2018

Thanks man, Changed

Code
with ss as requests.Session:
to
Code
with requests.Session() as ss:



RE: Function Returns None even though Return is stated - michalmonday - Sep 5, 2018

I guess the main problem is that exception is being ignored, then the function returns nothing and the user can't investigate what actually went wrong.

there's:
Code
try:
except:
   pass

You could replace it with:
Code
try:
except Exception as e:
   print(str(e))