2017-02-03 7 views
0

我使用的Protobuf對象的序列化,因爲我們需要從Java,CSHARP和Python客戶端連接到一個Java Web服務。執行期從使用Python肥皂水和與字節數組的protobuf的Java Web服務參數

如果我寫的Java protobuf的字節到一個文件中,我能夠在Python讀通過簡單地做:

f = open("../../../../resources/test.protoBytes", "rb") 
java_deserialized = timeSeriesRequests.TimeSeriesRequestMetaData() 
java_deserialized.ParseFromString(f.read()) 

如果我用Python寫一個文件:

temp = time_series_request_meta_data.SerializeToString() 
f = open("../../../../resources/test.protoBytes", "wb") 
f.write(temp) 
f.close() 

如果有在Java中讀取沒有問題。

使用無泡沫jurko 0.6我嘗試調用一個SOAP服務,做以字節[]在Java中:

client = Client('http://localhost:5750/ws/protoservice?wsdl', plugins=[LogPlugin()]) 
response = client.service.addValueToTimeSeries(data.SerializeToString(), otherData.SerializeToString()) 

服務器端的方法簽名:

public byte[] addValueToTimeSeries(byte[] protoRequestMetaData, byte[] protoRequest) 

我可以把一個破發指向服務器端調用,並調用該方法,但字節數組字符串不正確。

這是從客戶端發送:

b'<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://services.common.ddp/"><SOAP-ENV:Header/><ns0:Body><ns1:addValueToTimeSeries><arg0>b&apos;\\n\\x06ddp_sm\\x12\\x0btest-domain\\x1a\\tlocalhost&apos;</arg0><arg1>b&apos;\\n\\x06\\x08\\x81\\x9c\\x92\\xa5\\x05\\x12\\x06\\x08\\x81\\x9c\\x92\\xa5\\x05\\x1a\\x04Test&quot;\\x05Test2*&lt;\\n/type.googleapis.com/google.protobuf.DoubleValue\\x12\\t\\t\\x00\\x00\\x00\\x00\\x00\\[email protected]&apos;</arg1></ns1:addValueToTimeSeries></ns0:Body></SOAP-ENV:Envelope>' 

我試着這樣做,而不是:

response = client.service.addValueToTimeSeries(list(data.SerializeToString()), list(otherData.SerializeToString()) 

,我可以看到的是,在正確的字節數組創建客戶端,但現在對服務器側的字節數組爲空。

這是從Python的客戶端發送(從泡沫登錄):

b'<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://services.common.ddp/"><SOAP-ENV:Header/><ns0:Body><ns1:addValueToTimeSeries><arg0>10</arg0><arg0>6</arg0><arg0>100</arg0><arg0>100</arg0><arg0>112</arg0><arg0>95</arg0><arg0>115</arg0><arg0>109</arg0><arg0>18</arg0><arg0>11</arg0><arg0>116</arg0><arg0>101</arg0><arg0>115</arg0><arg0>116</arg0><arg0>45</arg0><arg0>100</arg0><arg0>111</arg0><arg0>109</arg0><arg0>97</arg0><arg0>105</arg0><arg0>110</arg0><arg0>26</arg0><arg0>9</arg0><arg0>108</arg0><arg0>111</arg0><arg0>99</arg0><arg0>97</arg0><arg0>108</arg0><arg0>104</arg0><arg0>111</arg0><arg0>115</arg0><arg0>116</arg0><arg1>10</arg1><arg1>6</arg1><arg1>8</arg1><arg1>129</arg1><arg1>156</arg1><arg1>146</arg1><arg1>165</arg1><arg1>5</arg1><arg1>18</arg1><arg1>6</arg1><arg1>8</arg1><arg1>129</arg1><arg1>156</arg1><arg1>146</arg1><arg1>165</arg1><arg1>5</arg1><arg1>26</arg1><arg1>4</arg1><arg1>84</arg1><arg1>101</arg1><arg1>115</arg1><arg1>116</arg1><arg1>34</arg1><arg1>5</arg1><arg1>84</arg1><arg1>101</arg1><arg1>115</arg1><arg1>116</arg1><arg1>50</arg1><arg1>42</arg1><arg1>60</arg1><arg1>10</arg1><arg1>47</arg1><arg1>116</arg1><arg1>121</arg1><arg1>112</arg1><arg1>101</arg1><arg1>46</arg1><arg1>103</arg1><arg1>111</arg1><arg1>111</arg1><arg1>103</arg1><arg1>108</arg1><arg1>101</arg1><arg1>97</arg1><arg1>112</arg1><arg1>105</arg1><arg1>115</arg1><arg1>46</arg1><arg1>99</arg1><arg1>111</arg1><arg1>109</arg1><arg1>47</arg1><arg1>103</arg1><arg1>111</arg1><arg1>111</arg1><arg1>103</arg1><arg1>108</arg1><arg1>101</arg1><arg1>46</arg1><arg1>112</arg1><arg1>114</arg1><arg1>111</arg1><arg1>116</arg1><arg1>111</arg1><arg1>98</arg1><arg1>117</arg1><arg1>102</arg1><arg1>46</arg1><arg1>68</arg1><arg1>111</arg1><arg1>117</arg1><arg1>98</arg1><arg1>108</arg1><arg1>101</arg1><arg1>86</arg1><arg1>97</arg1><arg1>108</arg1><arg1>117</arg1><arg1>101</arg1><arg1>18</arg1><arg1>9</arg1><arg1>9</arg1><arg1>0</arg1><arg1>0</arg1><arg1>0</arg1><arg1>0</arg1><arg1>0</arg1><arg1>0</arg1><arg1>89</arg1><arg1>64</arg1></ns1:addValueToTimeSeries></ns0:Body></SOAP-ENV:Envelope>' 

可有人請建議什麼,我需要做什麼?

僅供參考,這是我的第一個Python應用程序,因此,我不會感到驚訝,如果答案是微不足道的。這似乎很奇怪,我認爲這工作,如果通過文件來完成的,而是在整個網絡服務完成時失敗。

我現在已經做了一個小例子web服務,但不能上傳它出現的zip。

我一直在使用struct.unpack這樣的嘗試:

struct.unpack('B' * len(serialized_request), serialized_request) 

這將返回正確的int數組,而是通過對Web服務作爲空數組傳遞。

下面是WSDL的內容:

<definitions targetNamespace="http://examples.com/" name="com.examples.ExampleWebService"><types><xsd:schema><xsd:import namespace="http://examples.com/" schemaLocation="http://localhost:5750/ws/examplewebservice?xsd=1"/></xsd:schema></types><message name="addRequest"><part name="parameters" element="tns:addRequest"/></message><message name="addRequestResponse"><part name="parameters" element="tns:addRequestResponse"/></message><portType name="ExampleWebServiceContract"><operation name="addRequest"><input wsam:Action="http://examples.com/ExampleWebServiceContract/addRequestRequest" message="tns:addRequest"/><output wsam:Action="http://examples.com/ExampleWebServiceContract/addRequestResponse" message="tns:addRequestResponse"/></operation></portType><binding name="ExampleWebServicePortBinding" type="tns:ExampleWebServiceContract"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><operation name="addRequest"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="com.examples.ExampleWebService"><port name="ExampleWebServicePort" binding="tns:ExampleWebServicePortBinding"><soap:address location="http://localhost:5750/ws/examplewebservice"/></port></service></definitions> 
+0

我從來沒有在Java中編程過,但是從我的Python經驗 - SOAP在***中是痛苦的。 沒有WSDL,很難告訴你從Python方面調用服務的方式有什麼問題。 –

+0

「data」是什麼類型?也許'SerializeToString'是問題?看起來有一些不必要的編碼在那裏。 –

+0

V Stoykov - 我已經爲我創建的示例Web服務添加了wsdl的內容。 – Chill

回答

0

嘗試多種組合後,我發現,使用的Protobuf從Python中傳遞一個字節數組泡你需要做以下步驟:

  1. 在Protobuf對象上調用SerializeToString()
  2. 將字節的字符串表示形式傳遞給base64.b64encode(即導入base64模塊)
  3. 調用decode()將結果返回到ASCII,在開始時剝離b',並在結尾處。