M2PickleRPC.py :  » Web-Frameworks » Webware » Webware-1.0.2 » MiscUtils » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » MiscUtils » M2PickleRPC.py
"""M2Crypto-enhanced transport for PickleRPC

This lets you use M2Crypto for SSL encryption.

Based on m2xmlrpclib.py which is
Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved.

"""

import sys
from MiscUtils import StringIO
from PickleRPC import Transport
from M2Crypto import SSL,httpslib,m2urllib

__version__ = 1 # version of M2PickleRPC


class M2Transport(Transport):

  user_agent = "M2PickleRPC.py/%s - %s" % (__version__, Transport.user_agent)

  def __init__(self, ssl_context=None):
    if ssl_context is None:
      self.ssl_ctx = SSL.Context('sslv23')
    else:
      self.ssl_ctx = ssl_context

  def make_connection(self, host):
    _host, _port = m2urllib.splitport(host)
    if sys.version[0] == '2':
      return httpslib.HTTPS(_host, int(_port), ssl_context=self.ssl_ctx)
    elif sys.version[:3] == '1.5':
      return httpslib.HTTPS(self.ssl_ctx, _host, int(_port))
    else:
      raise RuntimeError, 'unsupported Python version'

  # @@ workarounds below are necessary because M2Crypto seems to
  # return from fileobject.read() early!  So we have to call it
  # over and over to get the full data.

  def parse_response(self, f):
    """Workaround M2Crypto issue mentioned above."""
    sio = StringIO()
    while 1:
      chunk = f.read()
      if not chunk:
        break
      sio.write(chunk)
    sio.seek(0)
    return Transport.parse_response(self, sio)

  def parse_response_gzip(self, f):
    """Workaround M2Crypto issue mentioned above."""
    sio = StringIO()
    while 1:
      chunk = f.read()
      if not chunk:
        break
      sio.write(chunk)
    sio.seek(0)
    return Transport.parse_response_gzip(self, sio)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.