Matchbox2093 Matchbox2093 - 4 months ago 12
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:

try:
print(URL)
os.system(
"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:

try:
print("Downloading file {} version: {}......".format(version, buildNo))
with requests.session() as s:
s.post(loginUrl, data="name:{}&password:{}".format(username,password))
print(URL)
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

You should use = instead : in your query:

s.post(loginUrl, data="name={}&password={}".format(username,password))

Also, you could (and should, following the requests best practice) proceed a dict to data argument:

s.post(
    loginUrl,
    data={
        'name': username,
        'password': password,
    }
)