Matchbox2093 Matchbox2093 - 1 year ago 114
Python Question

Using urllib instead of wget for session authentication

So currently i have a line of python code which uses wget and os.system to download a file from a website. However when i try to convert this wget to a more elegant library such as urllib or requests, the cookies fail to authenticate and instead downloads the login html page instead of the file.

Here is what i currently use:

"wget --save-cookies cookies.txt --keep-session-cookies --post-data='name={}&password={}' https://fakesite/login".format(
username, password))
os.system("wget --load-cookies cookies.txt --accept=exe {}".format(URL))

except Exception as e:
print(" File not found, please refer to the website manually for download link", e)

which i attempted to refactor into something like this:

print("Downloading file {} version: {}......".format(version, buildNo))
with requests.session() as s:, data="name:{}&password:{}".format(username,password))
r = s.get(URL,cookies=s.cookies, headers={"Accept": "application/octet-stream"})
print (r.content)

but this fails and instead prints the html content of the login page! can anyone shed some light on this?

Answer Source

You should use = instead : in your query:, data="name={}&password={}".format(username,password))

Also, you could (and should, following the requests best practice) proceed a dict to data argument:
        'name': username,
        'password': password,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download