Jul-04-2020, 07:17 AM
(This post was last modified: Jul-04-2020, 07:17 AM by RedLeonard.)
ok, now I'm trying to send a text file to the same server but it's not saving the text.
If I use this script to send to httbin.org it works fine:
now when I swap the url to post to my own webiste/server ('https://madmartin.pythonanywhere.com/postit2'
and then print out the headers, I only get:
so, is there a way to get my site acting the same way as the other one and save the file contents to EFL8.txt?
If I use this script to send to httbin.org it works fine:
import requests import json #url = 'https://madmartin.pythonanywhere.com/postit2' url = 'http://httpbin.org/post' textfile = {'file': open('why.txt', 'rb')} response = requests.post(url, files=textfile) response.status_code if response.status_code == 200: print('Success - file delivered! Raffik') print(response.content) else: print('Got unexpected status code {}: {!r}'.format(response.status_code, response.headers)) in Python idle, when entering 'response.headers' I get the read out:
Success - file delivered! Raffik b'{\n "args": {}, \n "data": "", \n "files": {\n "file": "Why can\'t I be you?"\n }, \n "form": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "162", \n "Content-Type": "multipart/form-data; boundary=36872c4d29134cfd97c0256735190e91", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.21.0", \n "X-Amzn-Trace-Id": "Root=1-5f002a57-c4e15fe0e86424b48e33f8ec"\n }, \n "json": null, \n "origin": "86.185.109.250", \n "url": "http://httpbin.org/post"\n}\n'that's fine, I can see the contents of the text file ('why can't I be you?')
now when I swap the url to post to my own webiste/server ('https://madmartin.pythonanywhere.com/postit2'
and then print out the headers, I only get:
{'Content-Length': '2', 'X-Clacks-Overhead': 'GNU Terry Pratchett', 'Date': 'Sat, 04 Jul 2020 07:05:08 GMT', 'Server': 'PythonAnywhere', 'Connection': 'keep-alive', 'Content-Type': 'text/html; charset=utf-8'}what I want is to be able to save the text file yet, like before, it only gives me an empty file, my view is as such:
@app.route('/postit2', methods=['GET', 'POST']) def postit2(): [python]x = request.files.getlist('textfile') #gives empty dict: x = request.files.getlist('textfile') with open('/home/MadMartin/datapostfiles/EFL8.txt', 'w') as f: f.write(str(x)) return '''ok'''EFL8.txt just returns []
so, is there a way to get my site acting the same way as the other one and save the file contents to EFL8.txt?