Python Requests script to login to a phpbb forum

L

Leo

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
 
C

Chris “Kwpolska†Warrick

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top