Bottom Page

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
Quote
#2
anybody have any ideas on this? thanks
Quote
#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.
Quote
#4
great, thanks a lot - that's fixed it!
I completely overlooked the json.dump method
Quote
#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?
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Need advice on pushing data to a server API msoultan 0 75 Jun-28-2020, 06:33 PM
Last Post: msoultan
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 123 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  Save Arduino data in mysql server via raspberrypi rithikvg 1 95 Jun-24-2020, 10:59 PM
Last Post: Larz60+
  Support required to data log in SQL server single table via raspbeery pi rithikvg 8 290 Jun-23-2020, 05:23 PM
Last Post: buran
  Get Python json data i n SQL Server Djin 1 169 May-13-2020, 05:56 AM
Last Post: ndc85430
  Moving large amount of data between MySql and Sql Server using Python ste80adr 4 209 Apr-24-2020, 01:24 PM
Last Post: Jeff900
  How to do the same as python -m requests.certs inside script? geekgeek 2 325 Feb-27-2020, 07:11 AM
Last Post: buran
  Why does Python/gpx save data as Latin1? Winfried 2 428 Nov-12-2019, 03:59 PM
Last Post: Winfried
  save data in .txt after certain interval Shaswat 1 346 Oct-13-2019, 07:07 AM
Last Post: Gribouillis
  Python requests writes the name of the file instead of contents to web page bluethundr 1 606 Jun-05-2019, 09:35 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)