1

I'm still very new to coding (been coding for a week) so I am struggling with a very basic function.

I am trying to log into a website using python however I am having a hard time changing the set-cookie header.

See my current code below:

import requests

targetURL = "http://hostip/v2_Website/aspx/login.aspx"

headers = {
"Host": "*host IP*",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"}

response = requests.get(url=targetURL,
                    proxies=proxies,
                    headers=headers,)

response_headers = response.headers

When I print the response.headers I get the following:

{'Cache-Control': 'no-cache, no-store', 'Pragma': 'no-cache,no-cache', 'Content-Length': '15671', 'Content-Type': 'text/html; charset=utf-8', 'Expires': '-1', 'Server': 'Microsoft-IIS/7.5', 'X-AspNet-Version': '2.0.50727', 'Set-Cookie': 'ASP.NET_SessionId=vq5q4lzlrqiiebbmxw341yic; path=/; HttpOnly, CookieLoginAttempts=5; expires=Tue, 14-Aug-2018 17:14:09 GMT; path=/', 'X-Powered-By': 'ASP.NET', 'Date': 'Tue, 14 Aug 2018 07:14:10 GMT', 'Connection': 'close'}

Obviously when I use these headers in my HTTP POST it fails due to the POST having a Set-Cookie header instead of Cookie value.

My objectives are as follows:

  • Update/change the Set-Cookie key to Cookie
  • Then I would like to remove values that are not needed in the Cookie key
  • Add other keys and values

Ultimately I would like to change the headers to the following so I can use it for my POST in order for me to pass login credentials:

POST /Test server/aspx/Login.aspx?function=Welcome HTTP/1.1
Host: *Host IP*
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
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
Referer: http://*HostIP*/v2_Website/aspx/main.aspx?function=Welcome
Cookie: ASP.NET_SessionId=3vy0fy55xsmffhbotikrwh55; CookieLoginAttempts=5; Admin=false
Connection: close
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
Content-Length: 220

Is my above objective even possible? If so how does one even achieve it as I don't understand the process of modifying a dictionary I can't see.

Again I would like to you to note I am still very green in the world of coding and trying to "think like a coder" thus keeping responses a little less technical would be highly appreciated, just so I can understand your response and advice. Any help would be great!

iacob
  • 20,084
  • 6
  • 92
  • 119
Chaplin
  • 45
  • 9
  • I noticed some down votes on my post. Can someone point out what I am doing wrong so I may rectify the issue? – Chaplin Aug 14 '18 at 08:08
  • Don't get discouraged. You actually took the time to write and ask a relatively clear question. Keep at it. I didn't downvote, but it's probably because you're asking a very specific thing that won't be too helpful for other people, and the question is a bit misguided. Consider: 1) HTTP response headers are different from HTTP request headers, and 2) 'requests' library has facilities for working with cookies directly anyway. So stop trying to "edit the dictionary" :-) – dkamins Aug 14 '18 at 08:21
  • Thanks dkamins, okay I will keep that in mind for my next post. Still very new to the coding world perhaps that is why my question is a bit misguided. I am not sure on the facilities for working with cookies directly, hopefully someone can point me in the right direction. – Chaplin Aug 14 '18 at 08:30
  • Check https://stackoverflow.com/a/4406521/2235381 – lojza Aug 14 '18 at 09:08

1 Answers1

0

I was a able to find the answer with a quite a bit of research.

Instead of trying to edit it manually I did the following:

import requests

session_requests = requests.session()

login_url = "http://*host ip*/v2_Website/aspx/Login.aspx"

result = session_requests.get(login_url)

result = session_requests.post(login_url,
                               headers= dict(referer=login_url))

This pulls through the needed cookie and adds everything as need. My headers come back as follows:

POST /_v2_Website/aspx/Login.aspx HTTP/1.1
Host: *host IP*
User-Agent: python-requests/2.18.4
Accept-Encoding: gzip, deflate
Accept: */*
Connection: close
referer: http://*hostIP*/v2_Website/aspx/Login.aspx
Cookie: ASP.NET_SessionId=3crqoo45hnn21anuqulmmr55; CookieLoginAttempts=5
Content-Length: 79
Content-Type: application/x-www-form-urlencoded
Chaplin
  • 45
  • 9