Python Forum
How to save Python Requests data sent to server?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save Python Requests data sent to server?
#5
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:

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?
Reply


Messages In This Thread
RE: How to save Python Requests data sent to server? - by RedLeonard - Jul-04-2020, 07:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 632 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  python requests library .JSON() error mHosseinDS86 6 3,750 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  In SQL Server, mix data types. shiv11 0 921 Sep-21-2022, 12:50 PM
Last Post: shiv11
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,288 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Save data frame to .csv df.to.csv() mcva 1 1,644 Feb-03-2022, 07:05 PM
Last Post: mcva
  Python requests oauth2 access token herobpv 6 4,136 Sep-27-2021, 06:54 PM
Last Post: herobpv
  How to take the tar backup files form remote server to local server sivareddy 0 1,989 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,242 Jul-02-2021, 02:19 PM
Last Post: xtc14
  How to save json data in a dataframe shantanu97 1 2,217 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Python Requests SSL Aussie 0 2,038 Jan-07-2021, 02:09 AM
Last Post: Aussie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020