Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
auth - URL
#1
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.

Attached Files

Thumbnail(s)
       
Reply
#2
Don't post images of code, use the insert python button and insert your code.
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IBM Watson: Handshake status 403 Forbidden or No section: 'auth' groschat 1 2,793 May-07-2021, 03:44 PM
Last Post: jefsummers
  ImportError: No module named pydrive.auth lknights1987 5 5,923 Jun-23-2020, 06:40 PM
Last Post: snippsat
  PUT with Basic Auth and urllib2 rootVIII 0 2,935 Feb-22-2019, 11:25 PM
Last Post: rootVIII
  Passing basic auth data between views nikos 0 1,885 Feb-13-2019, 09:32 PM
Last Post: nikos
  google-auth requirements.txt strange behavior randalpinto 3 3,733 Dec-21-2018, 02:03 AM
Last Post: Larz60+
  "Raise SMTPException("SMTP AUTH extension not supported by server.") NajeebUllah 3 7,975 Mar-16-2018, 09:45 PM
Last Post: nilamo
  social-auth-app-django wheel and dependables "six" metalray 2 2,913 Nov-27-2017, 08:16 AM
Last Post: metalray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020