Python Forum

Full Version: auth - URL
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone sorry to disturb you. I wanna to ask a question about the authentification with an URL. How to judge whether a group of id and password is true or not? Cause when inputs are correct, the file is downloaded with the URL, but when the inputs are incorrect, it goes to a KEYERROR[KeyError: 'content-disposition'] and what I want to do is to change this part into an "if-else". Thx a lot in advance for your help and your time!

I think I should add something right here:
def get_filename(content_disposition_header):
    #Finds the filename in the 'Content-Disposition' header
    return re.findall('filename="(.+)"', content_disposition_header)[0]
More details about my codes:
def loginIn(url, login, pwd, LOGIN_URL_SUFFIX, LOGIN_CHECK_URL_SUFFIX):
    login_url = url + LOGIN_URL_SUFFIX
    login_check_url = url + LOGIN_CHECK_URL_SUFFIX
    client = requests.session()
    login_res = client.get(login_url, verify=False)
    soup = BeautifulSoup(login_res.text, 'html.parser')
    csrf_token = soup.find('input', dict(name='_csrf_token'))['value']
    login_data = dict(_username=login, _password=pwd, _csrf_token=csrf_token)
    login_check_res = client.post(login_check_url, data=login_data)
    return client

def get_filename(content_disposition_header):
    #Finds the filename in the 'Content-Disposition' header
    return re.findall('filename="(.+)"', content_disposition_header)[0]

def get_data(client, url):
    #Exports the requested data
    export_url = build_url(url)
    data_res = client.get(export_url, allow_redirects=True)
    filename = get_filename(data_res.headers['Content-Disposition'])    #error
    with open(os.path.join(out, filename), 'wb') as f:
        f.write(data_res.content)
Thx a lot again.
Don't post images of code, use the insert python button and insert your code.
(May-19-2022, 03:18 PM)Axel_Erfurt Wrote: [ -> ]Don't post images of code, use the insert python button and insert your code.

Thank you for your reminding, I've changed the format.