Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66682 > unrolled thread

Python Requests script to login to a phpbb forum

Started byLeo <ttdhead@gmail.com>
First post2014-02-19 22:46 +1100
Last post2014-02-19 12:50 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Python Requests script to login to a phpbb forum Leo <ttdhead@gmail.com> - 2014-02-19 22:46 +1100
    Re: Python Requests script to login to a phpbb forum Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2014-02-19 12:50 +0100

#66682 — Python Requests script to login to a phpbb forum

FromLeo <ttdhead@gmail.com>
Date2014-02-19 22:46 +1100
SubjectPython Requests script to login to a phpbb forum
Message-ID<le25ak$2qq$1@dont-email.me>
Hi there,

I have decided to jump in at the deep end to try and learn python. I 
have been able to get requests to pull the login page off the server 
and I think send the login request. I don't get logged in though. 
Please let me know if I would be better off asking in a phpBB related 
group or forum.

Here is my code without username and password included:
import requests

payload = {'username': 'username', 'password': 'password'}
r = 
requests.post("http://www.tt-forums.net/ucp.php?mode=login",data=payload)
sidStart = r.text.find("sid")+4
sid = r.text[sidStart:sidStart+32]
parameters = {'mode': 'login', 'sid': sid}
r = 
requests.post("http://www.tt-forums.net/ucp.php",params=parameters,data=payload)
if "Logout" in r.text: print("We are in")

Thank you

-- 
ClassicVB Users Regroup! comp.lang.basic.visual.misc
Free usenet access at http://www.eternal-september.org

[toc] | [next] | [standalone]


#66684

FromChris “Kwpolska” Warrick <kwpolska@gmail.com>
Date2014-02-19 12:50 +0100
Message-ID<mailman.7145.1392810604.18130.python-list@python.org>
In reply to#66682
On Wed, Feb 19, 2014 at 12:46 PM, Leo <ttdhead@gmail.com> wrote:
> Hi there,
>
> I have decided to jump in at the deep end to try and learn python. I have
> been able to get requests to pull the login page off the server and I think
> send the login request. I don't get logged in though. Please let me know if
> I would be better off asking in a phpBB related group or forum.
> Here is my code without username and password included:

You need cookies.  In order to do it, simply use sessions instead of
the “global” API.  Code:

import requests

payload = {'username': 'username', 'password': 'password'}
session = requests.Session()
r = session.post("http://www.tt-forums.net/ucp.php?mode=login",data=payload)
sidStart = r.text.find("sid")+4
sid = r.text[sidStart:sidStart+32]
parameters = {'mode': 'login', 'sid': sid}
r = session.post("http://www.tt-forums.net/ucp.php",params=parameters,data=payload)
if "Logout" in r.text: print("We are in")

(for the record, I added the session creation line and replaced
requests. with session. so it uses your session.  This code was not
tested on a real phpBB forum; if everything you coded is correct, it
will work.)

-- 
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web