PHP xmlrpc客户端和Python 2.5 xmlrpc服务器:不完整数据和连接重置由对等错误

时间:2012-02-23 06:10:07

标签: php python xml-rpc

我有一个用Python运行的XML RPC服务器。

它是作为SimpleXMLRPCServer类的实例实现的。

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

class MyClass:
    def getGeneExtraInfo(self,genome,infoType,elements):
        print 'DEBUG:\ngenome: %s\ninfoType: %s, elements %s' % (genome,infoType,elements)
        return 'A' * 10000

csfServer = MyClass()

server = SimpleXMLRPCServer((serverHost, serverPort), SimpleXMLRPCRequestHandler)
server.register_instance(csfServer) 
server.serve_forever()

csfServer的方法def getGeneExtraInfo(self,genome,infoType,elements)返回一个长字符串,让我们考虑一下,为了简单起见,就是重写“A”重复10000次。

我通过以下代码通过PHP访问此Web服务:

function sendRequest($host, $url, $request, $port = 80) {                                                                                                                                      

  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  if ($socket === false) {
      echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
      return FALSE;
  }

  $address = gethostbyname($host);
  if (!socket_connect ($socket, $address, $port)) {
      echo socket_strerror(socket_last_error());
      return FALSE;
  }

  $httpQuery = "POST ". $url ." HTTP/1.0\r\n";
  $httpQuery .= "User-Agent: xmlrpc\r\n";
  $httpQuery .= "Host: ". $host ."\r\n";
  $httpQuery .= "Content-Type: text/xml\r\n";
  $httpQuery .= "Content-Length: ". strlen($request) ."\r\n\r\n";
  $httpQuery .= $request ."\r\n";

  if (!socket_send($socket, $httpQuery , strlen($httpQuery), 0)) {
      echo socket_strerror(socket_last_error());
      return FALSE;
  }

  $xmlResponse = "";
  $buff = "";
  while ($bytes = socket_recv($socket, $buff, 1024, MSG_WAITALL) > 0) {
      $xmlResponse .= $buff;
  }

  // Just for debugging
  echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";

  socket_close($socket);

  return  $xmlResponse;
}

$request变量是build:

$xmlrpc_output_options = array( 
                        "output_type" => "xml", 
                        "verbosity" => "no_white_space", 
                        "escaping" => array("markup", "non-ascii", "non-print"), 
                        "version" => "xmlrpc", 
                        "encoding" => "UTF-8");


$xmlRequest = xmlrpc_encode_request('getGeneExtraInfo', array($genome,$infoType,$elements), $xmlrpc_output_options);

(可以在以下C和Python代码中看到构建的请求)

当服务器使用python 2.4运行时,php客户端和服务器正常工作,但当使用python 2.5或2.6执行服务器时,有时(50%的时间),数据不完整且带有'连接由同行'错误重置

为了验证问题是用于Python还是PHP,我编写了两个客户端,一个在C中,另一个在Python中。两者都使用套接字,主要是PHP代码的工作方式。 “请求”内容是从PHP输出中复制的,这意味着完全是对PHP,C和Python的请求。

C代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

void error(const char *msg)
{
        perror(msg);
            exit(0);
}

int main(int argc, char *argv[])
{
        int sockfd, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;

        char* hostname=  "wks-13-15";
        int portno = 56572;

        char buffer[1024];

        char request[] = "POST / HTTP/1.0\r\nUser-Agent: xmlrpc\r\nHost: wks-13-15\r\nContent-Type: text/xml\r\nContent-Length: 4479\r\n\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>getGeneExtraInfo</methodName><params><param><value><string>hg19</string></value></param><param><value><string>GO</string></value></param><param><value><array><data><value><string>GO:0044428</string></value><value><string>GO:0044422</string></value><value><string>GO:0044425</string></value><value><string>GO:0044424</string></value><value><string>GO:0043412</string></value><value><string>GO:0006464</string></value><value><string>GO:0009889</string></value><value><string>GO:0003824</string></value><value><string>GO:0016020</string></value><value><string>GO:0016021</string></value><value><string>GO:0048522</string></value><value><string>GO:0048523</string></value><value><string>GO:0090304</string></value><value><string>GO:0019538</string></value><value><string>GO:0051171</string></value><value><string>GO:0001882</string></value><value><string>GO:0001883</string></value><value><string>GO:0080090</string></value><value><string>GO:0042221</string></value><value><string>GO:0048869</string></value><value><string>GO:0019222</string></value><value><string>GO:0005488</string></value><value><string>GO:0005886</string></value><value><string>GO:0005524</string></value><value><string>GO:0031090</string></value><value><string>GO:0050896</string></value><value><string>GO:0010556</string></value><value><string>GO:0010468</string></value><value><string>GO:0016740</string></value><value><string>GO:0003677</string></value><value><string>GO:2000112</string></value><value><string>GO:0005622</string></value><value><string>GO:0019219</string></value><value><string>GO:0006139</string></value><value><string>GO:0032502</string></value><value><string>GO:0032501</string></value><value><string>GO:0050794</string></value><value><string>GO:0009058</string></value><value><string>GO:0032991</string></value><value><string>GO:0044249</string></value><value><string>GO:0044260</string></value><value><string>GO:0044267</string></value><value><string>GO:0035639</string></value><value><string>GO:0009987</string></value><value><string>GO:0044464</string></value><value><string>GO:0051252</string></value><value><string>GO:0043170</string></value><value><string>GO:0005634</string></value><value><string>GO:0005737</string></value><value><string>GO:0050789</string></value><value><string>GO:0031326</string></value><value><string>GO:0051716</string></value><value><string>GO:0016787</string></value><value><string>GO:0031323</string></value><value><string>GO:0006810</string></value><value><string>GO:0048856</string></value><value><string>GO:0065007</string></value><value><string>GO:0043227</string></value><value><string>GO:0043167</string></value><value><string>GO:0044459</string></value><value><string>GO:0043169</string></value><value><string>GO:0008150</string></value><value><string>GO:0008152</string></value><value><string>GO:0006355</string></value><value><string>GO:0005575</string></value><value><string>GO:0046914</string></value><value><string>GO:0003674</string></value><value><string>GO:0006807</string></value><value><string>GO:0003676</string></value><value><string>GO:0044446</string></value><value><string>GO:0044444</string></value><value><string>GO:0051234</string></value><value><string>GO:0032555</string></value><value><string>GO:0043228</string></value><value><string>GO:0043229</string></value><value><string>GO:0043226</string></value><value><string>GO:0045449</string></value><value><string>GO:0032559</string></value><value><string>GO:0031224</string></value><value><string>GO:0017076</string></value><value><string>GO:0071842</string></value><value><string>GO:0071841</string></value><value><string>GO:0071840</string></value><value><string>GO:0060255</string></value><value><string>GO:0016043</string></value><value><string>GO:0034641</string></value><value><string>GO:0008270</string></value><value><string>GO:0000166</string></value><value><string>GO:0046872</string></value><value><string>GO:0044237</string></value><value><string>GO:0044238</string></value><value><string>GO:0043234</string></value><value><string>GO:0043231</string></value><value><string>GO:0043232</string></value><value><string>GO:0032553</string></value><value><string>GO:0005515</string></value><value><string>GO:0007165</string></value><value><string>GO:0048519</string></value><value><string>GO:0048518</string></value><value><string>GO:0030554</string></value></data></array></value></param></params></methodCall>";

        fprintf(stderr, "%s\n", request);

        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname(hostname);

        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }

        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr, 
                (char *)&serv_addr.sin_addr.s_addr,
                server->h_length);

        serv_addr.sin_port = htons(portno);

        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
            error("ERROR connecting");

        fprintf(stderr, "%d\n", strlen(request));

        n = write(sockfd,request,strlen(request));
        if (n < 0) 
            error("ERROR writing to socket");

        fprintf(stderr, "%d\n", n);

        bzero(buffer,1024);
        while (read(sockfd,buffer,1023) > 0) {
            printf("%s",buffer);
            bzero(buffer,1024);
        }

        if (n < 0) 
            error("ERROR reading from socket");

        close(sockfd);

        return 0;
}

python代码:

import socket

request = """POST / HTTP/1.0
User-Agent: xmlrpc
Host: wks-13-15
Content-Type: text/xml
Content-Length: 4479

<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>getGeneExtraInfo</methodName><params><param><value><string>hg19</string></value></param><param><value><string>GO</string></value></param><param><value><array><data><value><string>GO:0044428</string></value><value><string>GO:0044422</string></value><value><string>GO:0044425</string></value><value><string>GO:0044424</string></value><value><string>GO:0043412</string></value><value><string>GO:0006464</string></value><value><string>GO:0009889</string></value><value><string>GO:0003824</string></value><value><string>GO:0016020</string></value><value><string>GO:0016021</string></value><value><string>GO:0048522</string></value><value><string>GO:0048523</string></value><value><string>GO:0090304</string></value><value><string>GO:0019538</string></value><value><string>GO:0051171</string></value><value><string>GO:0001882</string></value><value><string>GO:0001883</string></value><value><string>GO:0080090</string></value><value><string>GO:0042221</string></value><value><string>GO:0048869</string></value><value><string>GO:0019222</string></value><value><string>GO:0005488</string></value><value><string>GO:0005886</string></value><value><string>GO:0005524</string></value><value><string>GO:0031090</string></value><value><string>GO:0050896</string></value><value><string>GO:0010556</string></value><value><string>GO:0010468</string></value><value><string>GO:0016740</string></value><value><string>GO:0003677</string></value><value><string>GO:2000112</string></value><value><string>GO:0005622</string></value><value><string>GO:0019219</string></value><value><string>GO:0006139</string></value><value><string>GO:0032502</string></value><value><string>GO:0032501</string></value><value><string>GO:0050794</string></value><value><string>GO:0009058</string></value><value><string>GO:0032991</string></value><value><string>GO:0044249</string></value><value><string>GO:0044260</string></value><value><string>GO:0044267</string></value><value><string>GO:0035639</string></value><value><string>GO:0009987</string></value><value><string>GO:0044464</string></value><value><string>GO:0051252</string></value><value><string>GO:0043170</string></value><value><string>GO:0005634</string></value><value><string>GO:0005737</string></value><value><string>GO:0050789</string></value><value><string>GO:0031326</string></value><value><string>GO:0051716</string></value><value><string>GO:0016787</string></value><value><string>GO:0031323</string></value><value><string>GO:0006810</string></value><value><string>GO:0048856</string></value><value><string>GO:0065007</string></value><value><string>GO:0043227</string></value><value><string>GO:0043167</string></value><value><string>GO:0044459</string></value><value><string>GO:0043169</string></value><value><string>GO:0008150</string></value><value><string>GO:0008152</string></value><value><string>GO:0006355</string></value><value><string>GO:0005575</string></value><value><string>GO:0046914</string></value><value><string>GO:0003674</string></value><value><string>GO:0006807</string></value><value><string>GO:0003676</string></value><value><string>GO:0044446</string></value><value><string>GO:0044444</string></value><value><string>GO:0051234</string></value><value><string>GO:0032555</string></value><value><string>GO:0043228</string></value><value><string>GO:0043229</string></value><value><string>GO:0043226</string></value><value><string>GO:0045449</string></value><value><string>GO:0032559</string></value><value><string>GO:0031224</string></value><value><string>GO:0017076</string></value><value><string>GO:0071842</string></value><value><string>GO:0071841</string></value><value><string>GO:0071840</string></value><value><string>GO:0060255</string></value><value><string>GO:0016043</string></value><value><string>GO:0034641</string></value><value><string>GO:0008270</string></value><value><string>GO:0000166</string></value><value><string>GO:0046872</string></value><value><string>GO:0044237</string></value><value><string>GO:0044238</string></value><value><string>GO:0043234</string></value><value><string>GO:0043231</string></value><value><string>GO:0043232</string></value><value><string>GO:0032553</string></value><value><string>GO:0005515</string></value><value><string>GO:0007165</string></value><value><string>GO:0048519</string></value><value><string>GO:0048518</string></value><value><string>GO:0030554</string></value></data></array></value></param></params></methodCall>"""

HOST = "wks-13-15"
PORT = 56572

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(request)

while 1:
        data = s.recv(1024)
        print data
        if not data: break

s.close()

两个程序都可以工作并接收整个响应数据。

我已经尝试更改编码,更改PHP读取数据的方式,但是不完整的数据和Connection by peer问题仍然存在。

我的问题:(随意回答其中任何一个:-))

  • 为什么C和Python程序有效,而PHP有时却没有?
  • 为什么有时PHP代码有效,有些则无效?
  • 为什么在python 2.4上运行服务器,PHP客户端代码有效,而python 2.5和2.6则没有?
  • 我该如何解决?欢迎提出建议。

1 个答案:

答案 0 :(得分:2)

我使用xml_rpc类,我不确定这个版本的PHP是从哪个开始的,但它对我有用。它甚至会对来自数组的请求进行编码,但是如果您已经为请求提供了XML,我会这样做:

PHP

function sendRequest($host, $url, $request, $port = 80) {

//create the context to send to the xmlrpc server
$context = stream_context_create(array('http' => array(
    'method' => "POST",
    'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
    'content' => $request
)));

//i am not sure how to get the url, normally something like http://server/api/xml
$server = "http://$host:$port"; //?

//store the response
$file = file_get_contents($server, false, $context);
//decode the response to xml
$return xmlrpc_decode($file);
}