python requests upload file

Viewed 25091

I'm visiting a website and I want to upload a file.

I wrote the code in python:

import requests

url = 'http://example.com'
files = {'file': open('1.jpg', 'rb')}
r = requests.post(url, files=files)
print(r.content)

But it seems no file has been uploaded, and the page is the same as the initial one.

I wonder how I could upload a file.

The source code of that page:

<html><head><meta charset="utf-8" /></head>

<body>
<br><br>
Upload<br><br>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="hidden" name="dir" value="/uploads/" />
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>
2 Answers
Related