简体   繁体   中英

Send and receive data from local client machine to a flask server on an Ubuntu server

Can someone please help me with my query.

On my local machine, I have a server and client implemented in Python. The JSON data is sent from client to server, the server parses the required data out of it and sends the result as JSON file back to the client. This is running fine on my local machine. I now want to implement this flask server on an Ubuntu server and then want to send and receive data. I am trying to use mod_wsgi as explained in

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps

I am still not able to get the data back to my local machine. Here is my code:

client.py

import sys
import json
import requests
import time
import os
import glob

data_location = 'C:\\Users\\cathy\\Desktop\\data'  # folder containing all the data        
for root, directories, files in os.walk(data_location):
    for directory  in directories:
        loc = (data_location + '/' + directory + '/*')
        all_files = glob.glob(loc)
        for filename in all_files:
            f=open(filename)
            f=f.read().splitlines()
            payload = {'input': f}
            s = json.dumps(payload)
            #res = requests.post("http://127.0.0.1:5000/my_data/", json=s).json()
            res = requests.post("http://12.345.678.890/my_data/", json=s).json()
            #time.sleep(10)
            if res['employee_id']:
                print(res['employee_id'])
            if res['name']:
                print(res['name'])

server.py

from flask import Flask
from flask import request
import json
import re
import sys
import os
import time
from parsers import id_parser, name_parser
import spacy
import re
from datetime import datetime#
nlp = spacy.load('en_core_web_lg')
import glob
app = Flask(__name__) 


@app.route('/my_data/', methods = ['POST'])
def parsing_data():
    jsondata = request.get_json()
    data = json.loads(jsondata)
    requiredData=data['input']
    employee_id_=id_parser(requiredData)
    name=name_parser(requiredData)

    result = {'employee_id_': employee_id_, 'name':name}
    return json.dumps(result)


if __name__ == '__main__':
    app.run(debug=True)

Instead of json.dump() try using flask jsonify method that will be compatible within flask.

#......
from flask import jsonify
#......


@app.route('/my_data/', methods = ['POST'])
def parsing_data():
    #.....

    result = {'employee_id_': employee_id_, 'name':name}
    return jsonify(result)

I'm not shure if it will help, but try it. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM