In a website with this code from flask:
from flask import Flask, request
app = Flask(__name__)
@app.route('/upload', methods=["POST", "GET"])
def upload():
return """
<html>
<body>
<h1>Guess what! I am a title!</h1>
<form action="/run" method="POST" name="upload">
<input type="password" name="password">
<input type="file" name="file" />
</form>
</body>
</html>"""
@app.route('/run', methods=["POST"])
def download_file():
if request.form['password'] == 'hey':
request.files['file'].save('/home/james/site.txt')
return "File uploaded successfully!"
else:
return "Wrong password"
How can I run a proper post request and get the output of /run
?
So far I've tried:
upload = {'password': 'whatever',
'file': open(my_files_location, 'rb')}
r = requests.post('http://jamesk.pythonanywhere.com/upload', data=upload)
But the website neither has run the form nor did it return what I wanted. This is what I get when I run r.content
:
b'\n <html>\n <body>\n <h1>Guess what! I am a title!</h1>\n <form action="/run" method="POST" name="upload">\n <input type="password" name="password">\n <input type="file" name="file" />\n </form>\n </body>\n </html>'
When I expected b'Wrong password'
Bad Request
\nThe browser (or proxy) sent a request that this server could not understand.
\n'`. Sorry for not putting this in the main question. – james kaly Jan 05 '17 at 13:39