0

I'm trying to use Requests in order to post messages to a phpBB3 forum. It's part of a larger project and this part is supposed to populate the forum with a few thousand posts from a database. I'm also using lxml to parse the pages when traversing the forum.

My problem is actually posting a post in the forums. First I login:

payload = {'username':'user', 'password':'password', 'login':'login', 
            'redirect':'index.php''}
url = 'http://test.com/test_forum'
q = s.post('http://test.com/test_forum/ucp.php?mode=login', data=payload)
a = s.get('http://test.com/test_forum')

After this I can see that my script is logged in. Then I use lxml to traverse the forum to the topic/thread that I want to post in and I use requests to GET the posting page, after which I use lxml again to obtain <input> values from the page and put them in a dictionary. Then I try POSTing that, in a similar fashion to what I did when logging in and nothing happens:

#This is after traversing the forum using lxml to get to the post page.
a = s.get('%s%s' % (url, links[0][1:]))
#a contains the response (posting page)

tree = html.fromstring(a.text)
#collect values from inputs in the form to submit along with message
form_token = tree.xpath('//input[@name="form_token"]/attribute::value')
subject = tree.xpath('//input[@name="subject"]/attribute::value')
topic_cur_post_id = tree.xpath('//input[@name="topic_cur_post_id"]/attribute::value')
lastclick = tree.xpath('//input[@name="lastclick"]/attribute::value')
creation_time = tree.xpath('//input[@name="creation_time"]/attribute::value')
fileupload = tree.xpath('//input[@name="fileupload"]/attribute::value')

data = {'form_token':form_token, 'subject':subject, 
'topic_cur_post_id':topic_cur_post_id, 'lastclick':lastclick, 
'creation_time':creation_time,
        'fileupload':fileupload, 'message':'TEST MESSAGE 2'}

q = s.post('http://vaultlog.frih.net/test_forum/posting.php?mode=reply&f=9&t=4',
             data=data)

When I check which page was loaded using print q.text, I see it's the same posting page again.

I took the liberty of using WireShark to see the different in packets. First, my script's POST message: http://pastebin.com/sRp9Zxme

And the message generated when I post using Firefox: http://pastebin.com/QSNawBRM

It seems the main difference is that naturally posting a message gives the POST a content-type: multipart/form-data; header whereas my script's header is Content-Type: application/x-www-form-urlencoded.

Another idea that comes to mind is that I'm not POSTing all of the required fields. I'm not submitting any checkboxes and such, but I'm submitting all the fields, including the hidden ones. This is the HTML of the form field: http://pastebin.com/U12BXZWF

What do I have to do in order to post to a forum using Requests? I will also have to post images as attachments, if this changes anything.

Protagonist
  • 492
  • 1
  • 6
  • 17
  • 1
    For your `multipart/form-data` VS `application/x-www-form-urlencoded`, see http://stackoverflow.com/questions/12385179/how-to-send-a-multipart-form-data-with-requests-in-python – Scharron Jan 22 '13 at 17:23
  • I tested it out really quick just now and yes, it changes the content type to `multipart/form-data`, however each entry in the packet now has this format:`Content-Disposition: form-data; name="message"; filename="message" Content-Type: application/x-wacomtabletplugin TEST MESSAGE 2` x-wactomtabletplugin? Do you think that this interfere with the posting, since it still didn't post anything? – Protagonist Jan 22 '13 at 17:34
  • Why do you have this `x-wactomtabletplugin` content-type ? I cannot see it anywhere in your previous pastes. – Scharron Jan 22 '13 at 17:46
  • I'm as surprised as you are, it wasn't there before. I do have wacom tablet drivers installed, but why in heavens would this show up there? Btw, thanks for the responses, I think I'm getting closer to the issue. – Protagonist Jan 22 '13 at 17:51
  • Well, I uninstalled everything wacom related, there aren't any wacom services nor processes running either. I'll try it out on my other machine. – Protagonist Jan 22 '13 at 18:03

0 Answers0