papezjustin
papezjustin

Reputation: 2355

How to Perform a HTTP Post "directly" in Python

I do not know much about the difference between a HTTP Get and a HTTP Post so I am hoping to get some information from those more knowledgeable then me.

I have written the following code:

<form action="https://na.leagueoflegends.com/user/login" method="post">
    <input name="name" type="text" value="MYACCOUNTLOGINUSERNAME">
    <input name="pass" type="password" value="PASSWORD">
    <input name="form_id" id="edit-user-login" value="user_login">
    <input class="login_button" value="Submit" type="submit" style="width:100px">            
</form>

When I pass my actual username and password to the form and click the submit button I will be properly logged into the website.

However, when I change method from POST to GET it returns the following: https://na.leagueoflegends.com/user/login?name=MYACCOUNTLOGINUSERNAME&pass=PASSWORD&form_id=user_login and when I click this link it does not log me in.

My question is, is it possible to do a "direct" POST via Python where I do not have to create a form but instead I can just open a URL that contains the proper parameters to log me in and POST them to the server?

Thank you in advance for your knowledge, suggestions, and/or answers.

Upvotes: 1

Views: 933

Answers (3)

Uku Loskit
Uku Loskit

Reputation: 42040

Logging in basically means receiving a cookie. After sending a POST request you get a cookie that authenticates you for future GET requests. A GET request, however, cannot authenticate you unless the server side has allowed this (it shoudld not be allowed, because it is very insecure). So the answer to your question is "no, not possible".

Inserting the cookie into the real browser is not very easy. I suggest you use mechanize, if you want to automate the login process and do nothing else automatically. If you want to automate other things as well look into urllib2 or requests for good HTTP request APIs.

Upvotes: 1

Johannes Charra
Johannes Charra

Reputation: 29913

The most sophisticated library to use for this purpose is probably requests

>>> import requests
>>> response = requests.post(... your args here ...)

Cf. the documentation of requests.post.

Upvotes: 2

solartic
solartic

Reputation: 4319

import urllib
import urllib2

url = 'https://na.leagueoflegends.com/user/login'
params = urllib.urlencode({
  'name': 'MYACCOUNTLOGINUSERNAME',
  'pass': 'PASSWORD',
  'form_id': 'user_login'
})
response = urllib2.urlopen(url, params).read()

urllib2 Tutorial

Upvotes: 1

Related Questions