views:

986

answers:

5

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.

+1  A: 

Your URL string shouldn't be

"http://www.woarl.com/board/ucp.php?mode=login"[logindata]

But

 "http://www.woarl.com/board/ucp.php?mode=login", logindata

I think, because [] is for array and it require an integer. I might be wrong cause I haven't done a lot of Python.

Daok
+1  A: 

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

S.Lott
+4  A: 

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.

Eli Courtwright
Invalid is a little vague. It's syntactically valid. It just has a type error because the index is a urlencoded dictionary, not an integer.
S.Lott
That's a good point; I've edited my response to say "semantically invalid", since as you point out it's valid syntactically.
Eli Courtwright
+1  A: 

If you do a type on logindata, you can see that it is a string:

>>> import urllib
>>> logindata = urllib.urlencode({'username': 'x', 'password': 'y'})
>>> type(logindata)
<type 'str'>

Putting it in brackets ([]) puts it in a list context, which isn't what you want.

Benjamin W. Smith
A: 

This would be easier with the high-level "mechanize" module.

RexE