3

I need to login and upload a file . The problem I am facing is, login page is different from uploading page. If I have to do it manually, I'll login (login.php) to the site and then navigate to the upload page (uploader.php) to upload the file. This what I have written:

import requests

url1='http://www.abc.com/login.php'
r = requests.post(url1, auth=('uname', 'pword'))
print r.status_code //msg:'200'

payload = {'upload':open('./tmp.txt')}
url2='http://www.abc.com/uploader.php'
r = requests.post(url2, data=payload)
print r.text //msg: "you must first login to upload the file"

My code is obviously not working as expected. Login part is working correctly but not uploading part. Please how can I accomplish my goal.

UPDATE:

To give more insight into my question, I am giving login.php and uploader.php file details:

login.php

<form method="POST" action="login.php" class="login">
<input type="text" name="username"></input>
<input type="password" name="password"></input>        
<input type="submit" value="Login"></input>

uploader.php

<form action='uploader.php' method='POST' id='form' class='upload' enctype="multipart/form-data" >
<input type='file' name='upload' id='file'></input>
<input type='button' value='Analyze' name='button' onclick='javascript: checkuploadform(false)'>
6
  • Where is the code you said you were going to post? Commented Jul 18, 2013 at 12:09
  • I have already posted it. why is it not visible? Commented Jul 18, 2013 at 12:10
  • Hey, you're doing a get requests to login.php when it should be post, according to above html. Commented Jul 18, 2013 at 12:11
  • Updated answer for some changes Commented Jul 18, 2013 at 12:15
  • yes, its post, I have edited it in the question. but still I am getting the same error message. Commented Jul 18, 2013 at 12:15

1 Answer 1

5

Make a session and then use that session to do your requests -

sessionObj = requests.session()
sessionOj.get(...) # Do whatever ...

A session persists your cookies for future requests.
And use post parameters for username,password as the parameters are required to login in login.php , not auth username password.
Also use files parameter to upload files. So the final code is -

import requests

sessionObj = requests.session()
url1='http://www.abc.com/login.php'
r = sessionObj.post(url1, params={'username':'usernamehere' , 'password':'password here'})
print r.status_code //msg:'200'


filehandle = open('./tmp.txt')
url2='http://www.abc.com/uploader.php'
r = sessionObj.post(url2, data={},files = {'upload':filehandle})
print r.text

Docs.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer. but, I am still getting that error. I have updated the question to give more insight into login.php and uploader.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.