python - 缓慢的网络服务问题

标签 python windows linux web-services silverlight-4.0

我在 Linux 机器 (ubuntu) 上创建了一个 Python 网络服务:

import soaplib
import os

from soaplib.core.service import rpc, DefinitionBase, soap
from soaplib.core.model.primitive import String, Integer
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import Array

def runcmd(cmd):
    fout = os.popen(cmd)
    out = fout.read()
    return out

class LinuxServices(DefinitionBase):
@soap(String, String,_returns=Array(String))
def df(self,server, user):
    L = []
    cmd = 'df -hP | grep "/"'
    output = runcmd(cmd).split('\n')
    for n in xrange(len(output)-1):
        out = output[n].split()
        L.append('%s;%s' % (out[5], out[4]))
    return L

if __name__=='__main__':
try:
    from wsgiref.simple_server import make_server
    soap_application = soaplib.core.Application([LinuxServices], 'tns')
    wsgi_application = wsgi.Application(soap_application)
    server = make_server('0.0.0.0', 7789, wsgi_application)
    server.serve_forever()
except ImportError:
    print "Error: example server code requires Python >= 2.5"

我是根据这个例子创建的:soaplib helloworld

然后(在 Windows 7 上)我创建了一个 Silverlight 项目,我在其中使用这个 ws 来获取我的 linux 服务器上的磁盘状态:

Silverlight 项目中的服务:

public class LinuxService
{
    [OperationContract]
    public List<dfItem> df()
    {
        List<dfItem> dfItems = new List<dfItem>();

        WebReference.Application app = new WebReference.Application();

        var result = app.df(new WebReference.df()/*...*/);

        foreach (var item in result.dfResult)
        {
            string[] info = item.Split(';');

            dfItem dfItem = new dfItem()
            {
                MountPoint = info[0].ToString(),
                Usage = info[1].ToString()
            };
            dfItems.Add(dfItem);
        }
        return dfItems;
    }
    //...
}

页面调用服务:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    LinuxServiceClient client = new LinuxServiceClient();

    client.dfCompleted += new EventHandler<dfCompletedEventArgs>(client_dfCompleted);

    client.dfAsync();
}

void client_dfCompleted(object sender, dfCompletedEventArgs e)
{
    DG.ItemsSource = e.Result;
    DG.Visibility = System.Windows.Visibility.Visible;
}

我的问题是,当我导航到此页面时,需要 4-8 秒才能从 ws(局域网中的 ws)获取数据。

我真的很怀疑线路带宽会造成这个等待时间......

我的问题: 您有什么建议可以加快速度吗?

系统信息:

  • Ubuntu服务器11.04

  • Python:Python 2.7

  • Soaplib:soaplib 2.0.0-beta2


  • Windows:Windows 7 sp1

  • 银光:银光 4

最佳答案

我建议使用 wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量的副本来“收听”网络上发生的对话。

当您开始捕获时,数据量似乎是巨大的,但是如果您可以发现任何看起来像您的 SOAP 消息的片段(应该很容易发现),那么您可以通过正确的方式快速过滤到该对话 -单击并选择“跟随 TCP 流”。

然后您可以在弹出窗口中看到您编写的 SOAP 服务与 silverlight 客户端之间的整个对话。

如果一切正常,请关闭弹出窗口。作为一个额外的好处,wireshark 将筛选出片段列表,只保留对话中的片段带有时间戳,以显示它们发生的时间。使用它来确定是客户端还是服务器响应缓慢。

如果看起来没有真正的延迟,那么我建议在要求 Silverlight 执行 SOAP 调用和它实际进行网络调用之间存在相当大的延迟。

关于python - 缓慢的网络服务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6213224/

相关文章:

linux - 在每个子目录中创建所有文件的 .txt

django - [FreeTDS][SQL Server]用户登录失败

python - 使用 python/BeautifulSoup 将 HTML 标记对替换为不同的标记对

python - 如何防止 pandas 仅将一个 df 的值分配给另一行的另一列?

windows - Windows XP 上的 Vc++ 2012 Express

c - Windows Defender 防病毒 API

linux - 从 Windows 到 Linux 的 RPC

python - 将 pandas dataframe 列从十六进制字符串转换为 int

python - 如何检测字符串后缀并从列表中删除这些后缀元素? - Python

c - 如何从 C 程序中获得 100% 的 CPU 使用率