如何使用bottle和wsgi发送压缩文件(zip文件)

时间:2019-01-09 17:36:03

标签: javascript wsgi bottle

我想使用WebSocket将压缩文件从服务器发送到客户端。 服务器端代码为:

@app.route('/websocket')
def handle_websocket():
    print("getting here")
    wsock = request.environ.get('wsgi.websocket')
    if not wsock:
        abort(400, 'Expected WebSocket request.')

    while True:
        try:
            # passing the information as a dictionary
            message = wsock.receive()
            data = json.loads(message)

            container = data[0]

            imag1 ='./download/png_files'
            newaddress_asc ='./download/asc_files'
            newaddress_tif ='./download/tif_files'

            # message is dictionary
            projCod = container["param1"]# EPSG code: 4326
            pntNumb = container["param2"]# point number


            dictionary=Com_pro.Dcomputing(projCod,pntNumb)

            writefile.loadresulttoaddress(dictionary,'png',imag1)


            zipfilename='D_result.zip'
            open_path ='./download/'
            save_path = open_path
            writefile.compress_save_file(zipfilename,open_path,save_path)
            filename='DSM_result.zip'
            static_file(filename, root='./download', download=filename)



            wsock.send(filename)
        except WebSocketError:
            break

from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketError
from geventwebsocket.handler import WebSocketHandler
server = WSGIServer(("0.0.0.0", 8081), app,
                    handler_class=WebSocketHandler)
server.serve_forever()

代码的客户端是:

function test(){
  var ws = new WebSocket("ws://example.com:8080/websocket");
  var clusternumber = 4;
  var vertex = {row: 11,
                value: "(-122.31088757514952,38.351500957145674)_(-122.30862379074097,38.352796664075726)_(-122.30824828147888,38.35236756889378)_(-122.30908513069151,38.35192164375559)_(-122.30877399444577,38.35158509466319)_(-122.30821609497069,38.351904816338134)_(-122.30743288993834,38.351055026670906)_(-122.30881690979002,38.35028095215651)_(-122.3073363304138,38.34822793185049)_(-122.30818390846251,38.34784088051842)_(-122.30822682380675,38.347849294699785)_"};
     // var vertex.value = [-122.19276309013367,38.13030324622909]
  var terrainatrribute = "Height,Curvature,Slope,Wetness,Position,";

  var soilfunction = "ClassMap,";   

  var data = [];
  ws.onopen = function() {
    data.push( {"param1": 'EPSG:4326', "param2": vertex.row,
           "param3":vertex.value, "param4":clusternumber,
           "param5":soilfunction, "param6":terrainatrribute,});

    console.log(JSON.stringify(data))
    ws.send(JSON.stringify(data));
  };

  ws.onmessage = function (evt) {
      console.log("This is is what is being returned from the server");
      alert(evt.data);
  };
  ws.onerror = function( evt ){
    alert(evt.data);
  }
}

对于字符串数据类型,代码可以完美运行。但是,当我尝试发送压缩文件时,它不起作用。我得到的返回数据类型为

  

[对象Blob]

我在bottle documentation在线阅读中说:“具有.read()方法的所有内容都被视为文件或类似文件的对象,并传递给WSGI服务器框架定义的wsgi.file_wrapper可调用对象一些WSGI服务器实现可以利用优化的系统调用(sendfile)来更有效地传输文件,而在其他情况下,这只是迭代 超过适合内存的块。不是可选的标头,例如Content-Length或Content-Type 自动设置。尽可能使用send_file()。 我对如何使用这些功能感到困惑。如果我必须实现自己的处理程序,您有什么建议。非常感谢您的帮助。

0 个答案:

没有答案
相关问题