使用python flask从客户端向服务器发送和接收XML文件

时间:2018-06-08 19:10:33

标签: python xml flask python-requests

我需要发送和接收存储在客户端计算机上的XML文件到烧瓶服务器。我能够使用请求对JSON文件执行相同的操作,但XML不会发生同样的情况。

这是我的服务器端代码

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')

这是客户端代码

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.")

我在服务器和主机上分别出现以下错误

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

客户端

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'))

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试匹配客户端和服务器的端口号。 而且,而不是写

req_data= request.get(silent=True) 

return jsonify(request.json)

并更改

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

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