Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python logging into a forum

I've written this to try and log onto a forum (phpBB3).

import urllib2, re
import urllib, re
logindata = urllib.urlencode({'username': 'x', 'password': 'y'})
page = urllib.urlopen("http://www.woarl.com/board/ucp.php?mode=login"[logindata])
output = page.read()

However when I run it it comes up with;

Traceback (most recent call last):
  File "C:/Users/Mike/Documents/python/test urllib2", line 4, in <module>
    page = urllib.urlopen("http://www.woarl.com/board/ucp.php?mode=login"[logindata])
TypeError: string indices must be integers

any ideas as to how to solve this?

edit

adding a comma between the string and the data gives this error instead

Traceback (most recent call last):
  File "C:/Users/Mike/Documents/python/test urllib2", line 4, in <module>
    page = urllib.urlopen("http://www.woarl.com/board/ucp.php?mode=login",[logindata])
  File "C:\Python25\lib\urllib.py", line 84, in urlopen
    return opener.open(url, data)
  File "C:\Python25\lib\urllib.py", line 192, in open
    return getattr(self, name)(url, data)
  File "C:\Python25\lib\urllib.py", line 327, in open_http
    h.send(data)
  File "C:\Python25\lib\httplib.py", line 711, in send
    self.sock.sendall(str)
  File "<string>", line 1, in sendall
TypeError: sendall() argument 1 must be string or read-only buffer, not list

edit2

I've changed the code from what it was to;

import urllib2, re
import urllib, re
logindata = urllib.urlencode({'username': 'x', 'password': 'y'})
page = urllib2.urlopen("http://www.woarl.com/board/ucp.php?mode=login", logindata)
output = page.read()

This doesn't throw any error messages, it just gives 3 blank lines. Is this because I'm trying to read from the log in page which disappears after logging in. If so how do I get it to display the index which is what should appear after hitting log in.

like image 350
user33061 Avatar asked Apr 06 '26 16:04

user33061


2 Answers

Your line

page = urllib.urlopen("http://www.woarl.com/board/ucp.php?mode=login"[logindata])

is semantically invalid Python. Presumably you meant

page = urllib.urlopen("http://www.woarl.com/board/ucp.php?mode=login", [logindata])

which has a comma separating the arguments. However, what you ACTUALLY want is simply

page = urllib2.urlopen("http://www.woarl.com/board/ucp.php?mode=login", logindata)

without trying to enclose logindata into a list and using the more up-to-date version of urlopen is the urllib2 library.

like image 172
Eli Courtwright Avatar answered Apr 09 '26 04:04

Eli Courtwright


How about using a comma between the string,"http:..." and the urlencoded data, [logindata]?

like image 43
S.Lott Avatar answered Apr 09 '26 06:04

S.Lott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!