Running python in a forum

I wrote this to try to enter the 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,

 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 on how to solve this?

change

adding a comma between the line 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 changed the code it was from

 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 does not cause any error messages, it just gives 3 empty lines. This is because I am trying to read from the login page, which disappears after the login. If so, how can I get it to display the index that should appear after logging in.

+3
source share
5 answers

Your line

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

is semantically invalid Python. Presumably you mean

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

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

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

without trying to list logindata and using a more modern version of urlopen, this is the urllib2 library.

+5
source

Your URL string should not 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 an array, and it requires an integer. Maybe I'm wrong because I did not do a lot of Python.

+1
source

How to use a comma between the line, "http:..." and urlencoded data, [logindata] ?

+1
source

If you make a type in logindata, you can see that it is a string:

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

Put it in brackets ([]) puts it in the list context, which is not what you want.

+1
source

It would be easier with the “mechanize” module at a high level.

-one
source

All Articles