设置Flask服务器时,我们可以尝试接收上传的文件用户
imagefile = flask.request.files['imagefile']
filename_ = str(datetime.datetime.now()).replace(' ', '_') + \
werkzeug.secure_filename(imagefile.filename)
filename = os.path.join(UPLOAD_FOLDER, filename_)
imagefile.save(filename)
logging.info('Saving to %s.', filename)
image = exifutil.open_oriented_im(filename)
Run Code Online (Sandbox Code Playgroud)
当我查看Klein文档时,我已经看到http://klein.readthedocs.io/en/latest/examples/staticfiles.html,但这似乎是从webservice提供文件而不是接收已上传到Web服务的文件.如果我想让我的Klein服务器能够接收abc.jpg并将其保存在文件系统中,是否有任何文档可以指导我实现该目标?
我的python Twisted Klein Web服务中有两个函数:
@inlineCallbacks
def logging(data):
ofile = open("file", "w")
ofile.write(data)
yield os.system("command to upload the written file")
@APP.route('/dostuff')
@inlineCallbacks
def dostuff():
yield logging(data)
print "check!"
returnValue("42")
Run Code Online (Sandbox Code Playgroud)
当os.system("command to upload the written file")运行时,它会显示消息说"开始上传",然后"上传完成".我想使日志记录功能异步,以便在logging处理dostuff程序打印出"check!" 之后处理器中的处理.(我实际上希望在returnValue("42")之后进行处理,但是我认为这两者都使得日志记录功能异步?)
我认为yield语句会使它无阻塞,但似乎并非如此,"检查!" 始终在"开始上传"和"上传完成"后打印.我很感激,如果有人可以给我一些反馈意见,因为我是异步编码的新手并且暂时被阻止了...
我在python中有一个简单的http客户端发送http post请求,如下所示:
import json
import urllib2
from collections import defaultdict as dd
data = dd(str)
req = urllib2.Request('http://myendpoint/test')
data["Input"] = "Hello World!"
response = urllib2.urlopen(req, json.dumps(data))
Run Code Online (Sandbox Code Playgroud)
在Flask的服务器端,我可以定义一个简单的函数
from flask import request
@app.route('/test', methods = ['POST'])
def test():
output = dd()
data = request.json
Run Code Online (Sandbox Code Playgroud)
并且dataon服务器将data与客户端上的字典相同.
但是,现在我要转向Klein,所以服务器代码如下所示:
@app.route('/test', methods = ['POST'])
@inlineCallbacks
def test(request):
output = dd()
data = request.json <=== This doesn't work
Run Code Online (Sandbox Code Playgroud)
并且在Klein中使用的请求不支持相同的功能.我想知道有没有办法让我在Klein中获得json,就像我在Flask中获得它一样?感谢您阅读此问题.