简体   繁体   中英

Send and recieve an XML file from client to server using python flask

I need to send and receive a XML file stored at a client machine to a flask server. I am able to do the same for JSON file using requests but the same is not happening with XML.

This is my server side code

app = Flask(__name__)
api = Api(app)
app.config["DEBUG"] = True


@app.route('/',methods=['GET','POST'])
def home():
    print ("************SERVER CALLED ***********")
    req_data= request.get(silent=True)

app.run(host='192.168.37.129')

and this is the client code

getlatestfile = os.getcwd()+fileseparator+"MSF"
getlatestfile=getlatestfile+fileseparator+"*.xml"
list_of_files = glob.glob(getlatestfile)
if list_of_files:
    msfFile = max(list_of_files, key=os.path.getctime)
    print ("[+] Sending latest MSF file which is..."+msfFile)
    with open(msfFile) as xml:

        response = requests.post('http://192.168.37.129:5000/', data=open(msfFile).read())
        print ('response from server:',response.text)

        #k=response.text
        #print(res1.content);
        print("[+] Received response from server...Intiating run of commands as directed by server \n")


else:
    print("No Result file present in the XML directory location/file could be corrupted.")

I am getting the following error on my server and host respectively

AttributeError: 'Request' object has no attribute 'get'

client

File "test.py", line 26, in <module>
    response = requests.post('http://192.168.37.129:5000/', data=open(msfFile).read())
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 490, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(32, 'Broken pipe'))

ANy help will be highly appreciated

try matching your port numbers for client and server. Also, instead of writing

req_data= request.get(silent=True) 

write

return jsonify(request.json)

and change

response = requests.post('http://192.168.37.129:5000/', data=open(msfFile).read())

to

response = requests.post('http://192.168.37.129:5000/', json=data)    

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