0

目前,我正在开发速度测试 CLI,并且我成功地使用 Jython 实现了 CLI。但是我遇到了一个问题,每次计算服务器端的互联网速度,我想消耗用户端的网络资源。所以任何人都知道这个java代码的正确实现方式是什么:

package com.speedtest.serviceimpl;
import java.io.InputStream;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import org.json.JSONException;
import org.json.JSONObject;
import org.python.util.PythonInterpreter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.speedtest.dto.SpeedTestResponse;
import com.speedtest.service.SpeedTestJythonService;

@Service
public class JythonService implements SpeedTestJythonService {

    private static final Logger Log =LoggerFactory.getLogger(JythonService.class);
    PythonInterpreter pythonInterpreter;
    public JythonService() {
        pythonInterpreter = new PythonInterpreter();
    }

    @Override
    public SpeedTestResponse execMethodInPyClass(InputStream inputStream,HttpServletRequest request) {
        try {
        StringWriter stringWriter = new StringWriter();
        Double upload_speed = new Double(0.0);
        Double download_speed = new Double(0.0);
        Double download_bytes = new Double(0.0);
        Double upload_bytes = new Double(0.0);
        String service_provider_name = new String();
        
        pythonInterpreter.setOut(stringWriter);
        pythonInterpreter.execfile(inputStream);
        
            System.out.println("Before Response==> "+stringWriter.toString());
            String res = stringWriter.toString().replace("|", "");
            System.out.println("Aftr Response==> "+res);
            JSONObject obj = new JSONObject(res);
            Double download = obj.getDouble("download");
            Double upload = obj.getDouble("upload");
            Double ping = obj.getDouble("ping");

            Long bytes_sent = obj.getLong("bytes_sent");

            Long bytes_received = obj.getLong("bytes_received");

            JSONObject json_server = obj.getJSONObject("server");

            String name = json_server.getString("name");

            String country_name = json_server.getString("country");

            JSONObject json_client = obj.getJSONObject("client");

            String isp = json_client.getString("isp");

            String ip = json_client.getString("ip");

            String country_code = json_client.getString("country");

            String isprating = json_client.getString("isprating");

            Log.info("obj", obj.toString());

            Log.info("download", download.toString());
            Log.info("upload", upload.toString());
            Log.info("ping", ping.toString());
            Log.info("bytes_sent", bytes_sent.toString());
            Log.info("bytes_received", bytes_received.toString());

            download_speed = (download / 1000.0 / 1000.0);
            upload_speed = (upload / 1000.0 / 1000.0);
            download_bytes = bytes_received.doubleValue();
            upload_bytes = bytes_sent.doubleValue();
            SpeedTestResponse response = new SpeedTestResponse();
            response.setDownload(download_speed);
            response.setIspName(isp);
            response.setPing(ping);
            response.setUpload(upload_speed);
            return response;
        } catch (JSONException  e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

    }
}

python代码是 SpeedTest.py

4

0 回答 0