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?
#1
Hi, Smile

Firstly, I am using Python 3.4, Flask and Python Anywhere server.

So, I am using Requests to send data to the server. This seems to work ok as a practice script but I'm having problems in saving the data to be able to observe it later. For now, just saving it in a basic text file would be fine.

Script (sent from Python Idle on laptop):

import requests
import json

response = requests.post('https://madmartin.pythonanywhere.com/postit', json={
    "language" : "German",
    "framework" : "Flask",
    
    "version_info" : {
        "python" : 3.4,
        "flask" : 0.12
    },
    "examples" : ["query", "form", "json"],
    "boolean_test" : True
})
response.status_code
   
if response.status_code == 200:
    print('Success- payload delivered!2020')
    print(response.content)
else:
    print('Got unexpected status code {}: {!r}'.format(response.status_code, response.headers))

Now, I'm not even sure it's possible to SAVE that data in the server as I've not added in any kind of password, token etc, but on sending that code, the headers are returned correctly (in Idle), as such:

Success- payload delivered!2020
b'\n           The framework value is: German\n           The language value is: Flask'
on the server (in flask_app.py) I have this code as the view function:

@app.route('/postit', methods=['GET', 'POST'])
def postit():

    req_data = request.get_json()
    language = req_data['language']
    framework = req_data['framework']
    python_version = req_data['version_info']['python'] #two keys are needed because of the nested object
    example = req_data['examples'][0] #an index is needed because of the array
    boolean_test = req_data['boolean_test']


    POST={}
    #args=sys.stdin.read()
    x = POST.get('framework')

    with open('/home/MadMartin/datapostfiles/EFL4.json', 'w') as f:
        f.write(str(x))
        f.close()


    return '''
           The framework value is: {}
           The language value is: {}'''.format(language, framework, x)
However, all this does is save an empty file(EFL4.json) - albeit with 'None' written in it.

So, is there any way to acutally save 'German' and 'Flask' on the server, or whatever is sent to it from Idle?

thanks in advance for any help offered
Reply
#2
anybody have any ideas on this? thanks
Reply
#3
You should post code that is possible to run,then is easier to help.
The json data is in req_data variable.
So if want to save the whole json return can use json dump like this.
from flask import Flask, render_template, jsonify, request
import json

app = Flask(__name__)
@app.route('/postit', methods=['GET', 'POST'])
def postit():
    req_data = request.get_json()
    language = req_data['language']
    framework = req_data['framework']
    python_version = req_data['version_info']['python'] #two keys are needed because of the nested object
    example = req_data['examples'][0] #an index is needed because of the array
    boolean_test = req_data['boolean_test']
    #POST={}
    #args=sys.stdin.read()   
    #x = POST.get('framework')
    print(req_data)
    # You most look at PythonAnywhere config on how to save file one there server
    with open('EFL4.json', 'w') as f:
        json.dump(req_data, f)

    return '''
           The framework value is: {}
           The language value is: {}'''.format(language, framework, x)

if __name__ == '__main__':
   app.run(debug=True)
Run with localhost to test this out.
import requests
import json

response = requests.post('http://127.0.0.1:5000/postit', json={
    "language" : "German",
    "framework" : "Flask",

    "version_info" : {
        "python" : 3.4,
        "flask" : 0.12
    },
    "examples" : ["query", "form", "json"],
    "boolean_test" : True
})
response.status_code

if response.status_code == 200:
    print('Success- payload delivered!2020')
    print(response.content)
else:
    print('Got unexpected status code {}: {!r}'.format(response.status_code, response.headers))
Now in EFL4.json.
Output:
{ "language": "German", "framework": "Flask", "version_info": { "python": 3.4, "flask": 0.12 }, "examples": [ "query", "form", "json" ], "boolean_test": true }
Quote:Firstly, I am using Python 3.4, Flask and Python Anywhere server.
3.4 is old now should at least use 3.6 or newer.
Reply
#4
great, thanks a lot - that's fixed it!
I completely overlooked the json.dump method
Reply
#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
#6
changing script to this:

@app.route('/postit2', methods=['GET', 'POST'])
def postit2():       
        d = request.files.to_dict()
        with open('/home/MadMartin/datapostfiles/EFL9.txt', 'w') as f:
            #read_data = f.read()
            f.write(str(d))           
            return '''ok''' 

saves this to EFL9:

{'file': <FileStorage: 'why.txt' (None)>}


so, doesn't look like the contents of the file can be sent in this way? Anyone know how to fix this? thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  python requests library .JSON() error mHosseinDS86 6 3,238 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  In SQL Server, mix data types. shiv11 0 854 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,176 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Save data frame to .csv df.to.csv() mcva 1 1,496 Feb-03-2022, 07:05 PM
Last Post: mcva
  Python requests oauth2 access token herobpv 6 3,771 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,871 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,115 Jul-02-2021, 02:19 PM
Last Post: xtc14
  How to save json data in a dataframe shantanu97 1 2,121 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Python Requests SSL Aussie 0 1,960 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