Exchange Web 服务 (EWS) - 通过 suds 进行 Exchange 2010 肥皂调用

发布于 2024-12-06 05:36:51 字数 4719 浏览 0 评论 0 原文

我尝试使用 suds 0.4.1 通过 Exchange Web 服务发送电子邮件:

import suds 
from suds.client import Client 
from suds.transport.https import WindowsHttpAuthenticated

url = "file:///C:/Services.wsdl" 
user = 'domain\\user' 
password = "hardtoguess"

ntlm = WindowsHttpAuthenticated(username=user,password=password) 
c = Client(url, transport=ntlm)

xml = ''' <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> <CreateItem     
MessageDisposition="SendAndSaveCopy"
xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
<SavedItemFolderId> <DistinguishedFolderId Id="sentitems"
xmlns="http://schemas.microsoft.com/exchange/services/2006/types"/> 
</SavedItemFolderId> <Items> <Message
xmlns="http://schemas.microsoft.com/exchange/services/2006/types">  
<ItemClass>IPM.Note</ItemClass> <Subject>Sent via Python->Exchange->EWS</Subject>  
<Body BodyType="Text">This message has been sent to you via Python,
Exchange and EWS :)</Body> <ToRecipients> <Mailbox> 
<EmailAddress>imran.azad@localhost</EmailAddress> </Mailbox> </ToRecipients>   
</Message> </Items> 
</CreateItem> </soap:Body></soap:Envelope>''' 

attr = c.service.CreateItem(__inject={'msg':xml})

xml = ''' <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <soap:Body>  
<ResolveNames
xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
ReturnFullContactData="true"> <UnresolvedEntry>azadi</UnresolvedEntry> 
</ResolveNames> </soap:Body> </soap:Envelope> ''' 

attr = c.service.ResolveNames(__inject={'msg':t})

 print attr 

我能够很好地进行身份验证,但是我不断收到以下错误:

Traceback (most recent call last):
  File "soap.py", line 10, in <module>
    c = Client(url, transport=ntlm)
  File "build\bdist.win32\egg\suds\client.py", line 112, in __init__
  File "build\bdist.win32\egg\suds\reader.py", line 152, in open
  File "build\bdist.win32\egg\suds\wsdl.py", line 159, in __init__
  File "build\bdist.win32\egg\suds\wsdl.py", line 220, in build_schema
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 93, in load
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 305, in open_imports
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 542, in open
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 563, in download
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 397, in instance
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 226, in __init__
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 305, in open_imports
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 542, in open
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 563, in download
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 397, in instance
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 226, in __init__
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 305, in open_imports
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 542, in open
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 560, in download
  File "build\bdist.win32\egg\suds\reader.py", line 79, in open
  File "build\bdist.win32\egg\suds\reader.py", line 95, in download
  File "build\bdist.win32\egg\suds\transport\https.py", line 60, in open
  File "build\bdist.win32\egg\suds\transport\http.py", line 62, in open
  File "build\bdist.win32\egg\suds\transport\http.py", line 118, in u2open
  File "C:\Python26\lib\urllib2.py", line 391, in open
    response = self._open(req, data)
  File "C:\Python26\lib\urllib2.py", line 409, in _open
    '_open', req)
  File "C:\Python26\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 1170, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python26\lib\urllib2.py", line 1143, in do_open
    r = h.getresponse()
  File "C:\Python26\lib\httplib.py", line 990, in getresponse
    response.begin()
  File "C:\Python26\lib\httplib.py", line 391, in begin
    version, status, reason = self._read_status()
  File "C:\Python26\lib\httplib.py", line 355, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine

任何建议将不胜感激。谢谢

Im trying to send an email via Exchange Web Services using suds 0.4.1:

import suds 
from suds.client import Client 
from suds.transport.https import WindowsHttpAuthenticated

url = "file:///C:/Services.wsdl" 
user = 'domain\\user' 
password = "hardtoguess"

ntlm = WindowsHttpAuthenticated(username=user,password=password) 
c = Client(url, transport=ntlm)

xml = ''' <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> <CreateItem     
MessageDisposition="SendAndSaveCopy"
xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
<SavedItemFolderId> <DistinguishedFolderId Id="sentitems"
xmlns="http://schemas.microsoft.com/exchange/services/2006/types"/> 
</SavedItemFolderId> <Items> <Message
xmlns="http://schemas.microsoft.com/exchange/services/2006/types">  
<ItemClass>IPM.Note</ItemClass> <Subject>Sent via Python->Exchange->EWS</Subject>  
<Body BodyType="Text">This message has been sent to you via Python,
Exchange and EWS :)</Body> <ToRecipients> <Mailbox> 
<EmailAddress>imran.azad@localhost</EmailAddress> </Mailbox> </ToRecipients>   
</Message> </Items> 
</CreateItem> </soap:Body></soap:Envelope>''' 

attr = c.service.CreateItem(__inject={'msg':xml})

xml = ''' <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> <soap:Body>  
<ResolveNames
xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
ReturnFullContactData="true"> <UnresolvedEntry>azadi</UnresolvedEntry> 
</ResolveNames> </soap:Body> </soap:Envelope> ''' 

attr = c.service.ResolveNames(__inject={'msg':t})

 print attr 

I'm able to authenticate fine, however I keep getting the following error:

Traceback (most recent call last):
  File "soap.py", line 10, in <module>
    c = Client(url, transport=ntlm)
  File "build\bdist.win32\egg\suds\client.py", line 112, in __init__
  File "build\bdist.win32\egg\suds\reader.py", line 152, in open
  File "build\bdist.win32\egg\suds\wsdl.py", line 159, in __init__
  File "build\bdist.win32\egg\suds\wsdl.py", line 220, in build_schema
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 93, in load
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 305, in open_imports
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 542, in open
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 563, in download
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 397, in instance
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 226, in __init__
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 305, in open_imports
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 542, in open
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 563, in download
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 397, in instance
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 226, in __init__
  File "build\bdist.win32\egg\suds\xsd\schema.py", line 305, in open_imports
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 542, in open
  File "build\bdist.win32\egg\suds\xsd\sxbasic.py", line 560, in download
  File "build\bdist.win32\egg\suds\reader.py", line 79, in open
  File "build\bdist.win32\egg\suds\reader.py", line 95, in download
  File "build\bdist.win32\egg\suds\transport\https.py", line 60, in open
  File "build\bdist.win32\egg\suds\transport\http.py", line 62, in open
  File "build\bdist.win32\egg\suds\transport\http.py", line 118, in u2open
  File "C:\Python26\lib\urllib2.py", line 391, in open
    response = self._open(req, data)
  File "C:\Python26\lib\urllib2.py", line 409, in _open
    '_open', req)
  File "C:\Python26\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 1170, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python26\lib\urllib2.py", line 1143, in do_open
    r = h.getresponse()
  File "C:\Python26\lib\httplib.py", line 990, in getresponse
    response.begin()
  File "C:\Python26\lib\httplib.py", line 391, in begin
    version, status, reason = self._read_status()
  File "C:\Python26\lib\httplib.py", line 355, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine

Any advice would be much appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2024-12-13 05:36:52

我也遇到过这个问题。但是,我只是间歇性地收到此错误。我发现另一篇文章提到了可能的解决方案,它对一些人有帮助(我得到的错误更少),但它没有完全解决问题。

问题似乎是 Exchange 服务器在发送有效响应之前关闭了连接。如果其他人可以添加更多内容,我将不胜感激。这已经让我发疯了。

也许这可以帮助你:
urllib2 正在抛出url 错误,而它在浏览器中正确打开

我不确定您是否已经这样做了,但我需要设置我的脚本来下载 messages.xsd, types.xsd 和 services.wsdl 本地并修补它。如下:

def setup(self):
    '''Patches the WSDL file to include a service tag. Returns path to
    local WSDL file.'''
    trans = self.transport
    try:
        makedirs(self.localdir)
    except OSError:
        # Directory already exists
        pass

    # Download messags.xsd file
    messages_url = '%s/messages.xsd' % self.urlprefix
    with open('%s/%s' % (self.localdir, 'messages.xsd'), 'w') as msg_xsd:
        msg_xsd.write(trans.geturl(messages_url, trans.authtype))

    # Download types.xsd file
    types_url = '%s/types.xsd' % self.urlprefix
    with open('%s/%s' % (self.localdir, 'types.xsd'), 'w') as types_xsd:
        types_xsd.write(trans.geturl(types_url, trans.authtype))

    # Modify WSDL to add service description
    service_url = '%s/Exchange.asmx' % self.urlprefix
    servicexml = '''
        <wsdl:service name="ExchangeServices">
            <wsdl:port name="ExchangeServicePort" binding="tns:ExchangeServiceBinding">
                  <soap:address location="%s"/>
                </wsdl:port>
          </wsdl:service>
        </wsdl:definitions>''' % service_url
    localwsdl = '%s/%s' % (self.localdir, 'Services.wsdl')
    wsdlxml = trans.geturl(self.wsdl, trans.authtype)
    with open(localwsdl, 'w') as wsdl:
        wsdl.write(wsdlxml.replace('</wsdl:definitions>', servicexml))

    return localwsdl

希望这将为您指明正确的方向。仅仅与 Exchange 进行泡沫对话一直是一个重大挑战。我在这里找到了一些帮助,以及为我的项目提供基础的示例代码:

http://lists.fedoraproject.org/pipermail/suds/2010-September/001144.html

更新:
使用 RequestServerVersion 发送正确的 SOAP 标头:

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope>
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2010"/>
    </soap:Header>
    <soap:Body>
    </soap:Body>
 </soap:Envelope>

为了简洁起见,我截断了 xml。

I've also been experiencing this issue. However, I only get this error intermittently. I found another post referring to a possible solution, and it helps some (I get the error less) but it hasn't fixed the issue completely.

The problem seems to be that the Exchange server closes the connection before sending a valid response. If anyone else can add more I would greatly appreciate it. This one has been driving me crazy.

Perhaps this may help you:
urllib2 is throwing an error for an url , while it's opening properly in browser

I'm not sure if you're doing it already, but I needed to setup my script to download the messages.xsd, types.xsd, and the services.wsdl locally and patch it. with the following:

def setup(self):
    '''Patches the WSDL file to include a service tag. Returns path to
    local WSDL file.'''
    trans = self.transport
    try:
        makedirs(self.localdir)
    except OSError:
        # Directory already exists
        pass

    # Download messags.xsd file
    messages_url = '%s/messages.xsd' % self.urlprefix
    with open('%s/%s' % (self.localdir, 'messages.xsd'), 'w') as msg_xsd:
        msg_xsd.write(trans.geturl(messages_url, trans.authtype))

    # Download types.xsd file
    types_url = '%s/types.xsd' % self.urlprefix
    with open('%s/%s' % (self.localdir, 'types.xsd'), 'w') as types_xsd:
        types_xsd.write(trans.geturl(types_url, trans.authtype))

    # Modify WSDL to add service description
    service_url = '%s/Exchange.asmx' % self.urlprefix
    servicexml = '''
        <wsdl:service name="ExchangeServices">
            <wsdl:port name="ExchangeServicePort" binding="tns:ExchangeServiceBinding">
                  <soap:address location="%s"/>
                </wsdl:port>
          </wsdl:service>
        </wsdl:definitions>''' % service_url
    localwsdl = '%s/%s' % (self.localdir, 'Services.wsdl')
    wsdlxml = trans.geturl(self.wsdl, trans.authtype)
    with open(localwsdl, 'w') as wsdl:
        wsdl.write(wsdlxml.replace('</wsdl:definitions>', servicexml))

    return localwsdl

Hopefully this will point you in the right direction. Simply talking to Exchange with suds has been a major challenge. I found some help here, as well as the sample code that has provided the base for my project:

http://lists.fedoraproject.org/pipermail/suds/2010-September/001144.html

Update:
Sending the correct SOAP headers with RequestServerVersion:

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope>
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2010"/>
    </soap:Header>
    <soap:Body>
    </soap:Body>
 </soap:Envelope>

I've truncated the xml for brevity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文