为什么在使用 python suds 连接到 Microsoft CRM 时出现连接错误/超时?
当我尝试使用 suds/python-ntlm 连接到 MS CRM Web 服务时,请求超时。但是,我尝试替换的代码(调用 cURL 命令行应用程序执行相同的调用)成功了。
显然,cURL 发送命令数据的方式有所不同,但如果我知道其中的区别,那我就该死了。以下是各种通话的完整详细信息。有人有任何提示吗?
这是发出请求的代码,后面是输出。 cURL 命令代码如下,其响应如下。当然,为了保护无辜者,主机、用户和密码都已更改。
wsdl_url = 'https://client.service.host/MSCrmServices/2007/MetadataService.asmx?WSDL'
username = r'domain\user.name'
password = 'userpass'
from suds.transport.https import WindowsHttpAuthenticated
from suds.client import Client
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
ntlmTransport = WindowsHttpAuthenticated(username=username, password=password)
metadata_client = Client(wsdl_url, transport=ntlmTransport)
request = metadata_client.factory.create('RetrieveAttributeRequest')
request.MetadataId = '00000000-0000-0000-0000-000000000000'
request.EntityLogicalName = 'opportunity'
request.LogicalName = 'new_typeofcontact'
request.RetrieveAsIfPublished = 'false'
attr = metadata_client.service.Execute(request)
print attr
这是输出:
DEBUG:suds.client:sending to (http://client.service.host/MSCrmServices/2007/MetadataService.asmx)
message:
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/crm/2007/WebServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:Execute>
<ns1:Request xsi:type="ns1:RetrieveAttributeRequest">
<ns1:MetadataId>00000000-0000-0000-0000-000000000000</ns1:MetadataId>
<ns1:EntityLogicalName>opportunity</ns1:EntityLogicalName>
<ns1:LogicalName>new_typeofcontact</ns1:LogicalName>
<ns1:RetrieveAsIfPublished>false</ns1:RetrieveAsIfPublished>
</ns1:Request>
</ns1:Execute>
</ns0:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': u'"http://schemas.microsoft.com/crm/2007/WebServices/Execute"', 'Content-Type': 'text/xml'}
DEBUG:suds.transport.http:sending:
URL:http://client.service.host/MSCrmServices/2007/MetadataService.asmx
HEADERS: {'SOAPAction': u'"http://schemas.microsoft.com/crm/2007/WebServices/Execute"', 'Content-Type': 'text/xml', 'Content-type': 'text/xml', 'Soapaction': u'"http://schemas.microsoft.com/crm/2007/WebServices/Execute"'}
MESSAGE:
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/crm/2007/WebServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:Execute>
<ns1:Request xsi:type="ns1:RetrieveAttributeRequest">
<ns1:MetadataId>00000000-0000-0000-0000-000000000000</ns1:MetadataId>
<ns1:EntityLogicalName>opportunity</ns1:EntityLogicalName>
<ns1:LogicalName>new_typeofcontact</ns1:LogicalName>
<ns1:RetrieveAsIfPublished>false</ns1:RetrieveAsIfPublished>
</ns1:Request>
</ns1:Execute>
</ns0:Body>
</SOAP-ENV:Envelope>
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (16, 0))
---------------------------------------------------------------------------
URLError Traceback (most recent call last)
/Users/crose/projects/2366/crm/<ipython console> in <module>()
/var/folders/nb/nbJAzxR1HbOppPcs6xO+dE+++TY/-Tmp-/python-67186icm.py in <module>()
19 request.LogicalName = 'new_typeofcontact'
20 request.RetrieveAsIfPublished = 'false'
21
---> 22 attr = metadata_client.service.Execute(request)
23 print attr
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/client.pyc in __call__(self, *args, **kwargs)
537 return (500, e)
538 else:
--> 539 return client.invoke(args, kwargs)
540
541 def faults(self):
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/client.pyc in invoke(self, args, kwargs)
596 self.method.name, timer)
597 timer.start()
--> 598 result = self.send(msg)
599 timer.stop()
600 metrics.log.debug(
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/client.pyc in send(self, msg)
621 request = Request(location, str(msg))
622 request.headers = self.headers()
--> 623 reply = transport.send(request)
624 if retxml:
625 result = reply.message
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/transport/https.pyc in send(self, request)
62 def send(self, request):
63 self.addcredentials(request)
---> 64 return HttpTransport.send(self, request)
65
66 def addcredentials(self, request):
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/transport/http.pyc in send(self, request)
75 request.headers.update(u2request.headers)
76 log.debug('sending:\n%s', request)
---> 77 fp = self.u2open(u2request)
78 self.getcookies(fp, u2request)
79 result = Reply(200, fp.headers.dict, fp.read())
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/transport/http.pyc in u2open(self, u2request)
116 return url.open(u2request)
117 else:
--> 118 return url.open(u2request, timeout=tm)
119
120 def u2opener(self):
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in open(self, fullurl, data, timeout)
381 req = meth(req)
382
--> 383 response = self._open(req, data)
384
385 # post-process response
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in _open(self, req, data)
399 protocol = req.get_type()
400 result = self._call_chain(self.handle_open, protocol, protocol +
--> 401 '_open', req)
402 if result:
403 return result
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
359 func = getattr(handler, meth_name)
360
--> 361 result = func(*args)
362 if result is not None:
363 return result
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in http_open(self, req)
1128
1129 def http_open(self, req):
-> 1130 return self.do_open(httplib.HTTPConnection, req)
1131
1132 http_request = AbstractHTTPHandler.do_request_
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in do_open(self, http_class, req)
1103 r = h.getresponse()
1104 except socket.error, err: # XXX what error?
-> 1105 raise URLError(err)
1106
1107 # Pick apart the HTTPResponse object to get the addinfourl
URLError: <urlopen error [Errno 60] Operation timed out>
cURL 命令是:
/opt/local/bin/curl --ntlm -u "domain\user.name:userpass" -k -d @- -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.1)" -H "Connection: Keep-Alive" -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction: http://schemas.microsoft.com/crm/2007/WebServices/Execute" https://client.service.host/MSCrmServices/2007/MetadataService.asmx
通过管道传输到 cURL 命令的数据:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">0</AuthenticationType>
<CrmTicket xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes"></CrmTicket>
<OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">CMIFS</OrganizationName>
<CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>
</CrmAuthenticationToken>
</soap:Header>
<soap:Body>
<Execute xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<Request xsi:type="RetrieveAttributeRequest">
<MetadataId>00000000-0000-0000-0000-000000000000</MetadataId>
<EntityLogicalName>opportunity</EntityLogicalName>
<LogicalName>new_typeofcontact</LogicalName>
<RetrieveAsIfPublished>false</RetrieveAsIfPublished>
</Request>
</Execute>
</soap:Body>
</soap:Envelope>
这是响应:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ExecuteResponse
xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<Response xsi:type="RetrieveAttributeResponse">
<AttributeMetadata xsi:type="PicklistAttributeMetadata">
<MetadataId>101346cf-a6af-4eb4-a4bf-9c3c6bbd6582</MetadataId>
<SchemaName>New_TypeofContact</SchemaName>
<LogicalName>new_typeofcontact</LogicalName>
<EntityLogicalName>opportunity</EntityLogicalName>
<AttributeType>
<Value>Picklist</Value>
</AttributeType>
<!-- stuff here -->
</AttributeMetadata>
</Response>
</ExecuteResponse>
</soap:Body>
</soap:Envelope>
When I try to connect to an MS CRM web service using suds/python-ntlm, I am getting a timeout on requests. However, the code that I'm trying to replace -- which calls out to the cURL command line app to do the same call -- succeeds.
Clearly something is different in the way that cURL is sending the command data, but I'll be damned if I know what the difference is. Below are the full details of the various calls. Anyone got any tips?
Here's the code that is making the request, followed by the output. The cURL command code is below that, and its response follows. Hosts, users, and passwords have been changed to protect the innocent, of course.
wsdl_url = 'https://client.service.host/MSCrmServices/2007/MetadataService.asmx?WSDL'
username = r'domain\user.name'
password = 'userpass'
from suds.transport.https import WindowsHttpAuthenticated
from suds.client import Client
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
ntlmTransport = WindowsHttpAuthenticated(username=username, password=password)
metadata_client = Client(wsdl_url, transport=ntlmTransport)
request = metadata_client.factory.create('RetrieveAttributeRequest')
request.MetadataId = '00000000-0000-0000-0000-000000000000'
request.EntityLogicalName = 'opportunity'
request.LogicalName = 'new_typeofcontact'
request.RetrieveAsIfPublished = 'false'
attr = metadata_client.service.Execute(request)
print attr
Here's the output:
DEBUG:suds.client:sending to (http://client.service.host/MSCrmServices/2007/MetadataService.asmx)
message:
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/crm/2007/WebServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:Execute>
<ns1:Request xsi:type="ns1:RetrieveAttributeRequest">
<ns1:MetadataId>00000000-0000-0000-0000-000000000000</ns1:MetadataId>
<ns1:EntityLogicalName>opportunity</ns1:EntityLogicalName>
<ns1:LogicalName>new_typeofcontact</ns1:LogicalName>
<ns1:RetrieveAsIfPublished>false</ns1:RetrieveAsIfPublished>
</ns1:Request>
</ns1:Execute>
</ns0:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': u'"http://schemas.microsoft.com/crm/2007/WebServices/Execute"', 'Content-Type': 'text/xml'}
DEBUG:suds.transport.http:sending:
URL:http://client.service.host/MSCrmServices/2007/MetadataService.asmx
HEADERS: {'SOAPAction': u'"http://schemas.microsoft.com/crm/2007/WebServices/Execute"', 'Content-Type': 'text/xml', 'Content-type': 'text/xml', 'Soapaction': u'"http://schemas.microsoft.com/crm/2007/WebServices/Execute"'}
MESSAGE:
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/crm/2007/WebServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns0:Body>
<ns1:Execute>
<ns1:Request xsi:type="ns1:RetrieveAttributeRequest">
<ns1:MetadataId>00000000-0000-0000-0000-000000000000</ns1:MetadataId>
<ns1:EntityLogicalName>opportunity</ns1:EntityLogicalName>
<ns1:LogicalName>new_typeofcontact</ns1:LogicalName>
<ns1:RetrieveAsIfPublished>false</ns1:RetrieveAsIfPublished>
</ns1:Request>
</ns1:Execute>
</ns0:Body>
</SOAP-ENV:Envelope>
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (16, 0))
---------------------------------------------------------------------------
URLError Traceback (most recent call last)
/Users/crose/projects/2366/crm/<ipython console> in <module>()
/var/folders/nb/nbJAzxR1HbOppPcs6xO+dE+++TY/-Tmp-/python-67186icm.py in <module>()
19 request.LogicalName = 'new_typeofcontact'
20 request.RetrieveAsIfPublished = 'false'
21
---> 22 attr = metadata_client.service.Execute(request)
23 print attr
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/client.pyc in __call__(self, *args, **kwargs)
537 return (500, e)
538 else:
--> 539 return client.invoke(args, kwargs)
540
541 def faults(self):
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/client.pyc in invoke(self, args, kwargs)
596 self.method.name, timer)
597 timer.start()
--> 598 result = self.send(msg)
599 timer.stop()
600 metrics.log.debug(
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/client.pyc in send(self, msg)
621 request = Request(location, str(msg))
622 request.headers = self.headers()
--> 623 reply = transport.send(request)
624 if retxml:
625 result = reply.message
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/transport/https.pyc in send(self, request)
62 def send(self, request):
63 self.addcredentials(request)
---> 64 return HttpTransport.send(self, request)
65
66 def addcredentials(self, request):
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/transport/http.pyc in send(self, request)
75 request.headers.update(u2request.headers)
76 log.debug('sending:\n%s', request)
---> 77 fp = self.u2open(u2request)
78 self.getcookies(fp, u2request)
79 result = Reply(200, fp.headers.dict, fp.read())
/Users/crose/virtualenv/advanis/lib/python2.6/site-packages/suds/transport/http.pyc in u2open(self, u2request)
116 return url.open(u2request)
117 else:
--> 118 return url.open(u2request, timeout=tm)
119
120 def u2opener(self):
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in open(self, fullurl, data, timeout)
381 req = meth(req)
382
--> 383 response = self._open(req, data)
384
385 # post-process response
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in _open(self, req, data)
399 protocol = req.get_type()
400 result = self._call_chain(self.handle_open, protocol, protocol +
--> 401 '_open', req)
402 if result:
403 return result
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
359 func = getattr(handler, meth_name)
360
--> 361 result = func(*args)
362 if result is not None:
363 return result
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in http_open(self, req)
1128
1129 def http_open(self, req):
-> 1130 return self.do_open(httplib.HTTPConnection, req)
1131
1132 http_request = AbstractHTTPHandler.do_request_
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.pyc in do_open(self, http_class, req)
1103 r = h.getresponse()
1104 except socket.error, err: # XXX what error?
-> 1105 raise URLError(err)
1106
1107 # Pick apart the HTTPResponse object to get the addinfourl
URLError: <urlopen error [Errno 60] Operation timed out>
The cURL command is:
/opt/local/bin/curl --ntlm -u "domain\user.name:userpass" -k -d @- -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.1)" -H "Connection: Keep-Alive" -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction: http://schemas.microsoft.com/crm/2007/WebServices/Execute" https://client.service.host/MSCrmServices/2007/MetadataService.asmx
The data that is piped to that cURL command:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">0</AuthenticationType>
<CrmTicket xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes"></CrmTicket>
<OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">CMIFS</OrganizationName>
<CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>
</CrmAuthenticationToken>
</soap:Header>
<soap:Body>
<Execute xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<Request xsi:type="RetrieveAttributeRequest">
<MetadataId>00000000-0000-0000-0000-000000000000</MetadataId>
<EntityLogicalName>opportunity</EntityLogicalName>
<LogicalName>new_typeofcontact</LogicalName>
<RetrieveAsIfPublished>false</RetrieveAsIfPublished>
</Request>
</Execute>
</soap:Body>
</soap:Envelope>
Here's the response:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ExecuteResponse
xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<Response xsi:type="RetrieveAttributeResponse">
<AttributeMetadata xsi:type="PicklistAttributeMetadata">
<MetadataId>101346cf-a6af-4eb4-a4bf-9c3c6bbd6582</MetadataId>
<SchemaName>New_TypeofContact</SchemaName>
<LogicalName>new_typeofcontact</LogicalName>
<EntityLogicalName>opportunity</EntityLogicalName>
<AttributeType>
<Value>Picklist</Value>
</AttributeType>
<!-- stuff here -->
</AttributeMetadata>
</Response>
</ExecuteResponse>
</soap:Body>
</soap:Envelope>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您在 WSDL 中的位置可能有问题。如果您在显示正在发送时检查调试输出:
请注意,URL 是 http,并且与初始连接(https)不同。
尝试通过添加位置参数来修改客户端的实例化,以强制其使用 https,如下所示:(
请注意,服务 URL 应该与 WSDL URL 减去
?WSDL
相同)。It looks like you might be having a problem with the location in the WSDL. If you check the debug output when it says it is sending:
Note that the URL is http, and that does not vibe with the initial connection, which was https.
Try modifying the instantiation of your client by adding a location argument to force it to use https like so:
(Note that the service URL should be the same of as the WSDL URL minus the
?WSDL
).添加
cache=None
实际上可以达到目的。Adding
cache=None
actually do the trick.