Package restkit :: Module sock
[hide private]
[frames] | no frames]

Source Code for Module restkit.sock

  1  # -*- coding: utf-8 - 
  2  # 
  3  # This file is part of restkit released under the MIT license.  
  4  # See the NOTICE for more information. 
  5   
  6  import socket 
  7  import sys 
  8   
  9   
 10  CHUNK_SIZE = 16 * 1024  
 11  MAX_BODY = 1024 * 112 
 12  DNS_TIMEOUT = 60 
 13    
 14  _allowed_ssl_args = ('keyfile', 'certfile', 'server_side', 
 15                      'cert_reqs', 'ssl_version', 'ca_certs',  
 16                      'do_handshake_on_connect', 'suppress_ragged_eofs') 
 17   
18 -def validate_ssl_args(ssl_args):
19 for arg in ssl_args: 20 if arg not in _allowed_ssl_args: 21 raise TypeError('connect() got an unexpected keyword argument %r' % arg)
22
23 -def close(skt):
24 if not skt or not hasattr(skt, "close"): return 25 try: 26 skt.close() 27 except socket.error: 28 pass
29
30 -def send_chunk(sock, data):
31 chunk = "".join(("%X\r\n" % len(data), data, "\r\n")) 32 sock.sendall(chunk)
33
34 -def send(sock, data, chunked=False):
35 if chunked: 36 return send_chunk(sock, data) 37 sock.sendall(data)
38
39 -def send_nonblock(sock, data, chunked=False):
40 timeout = sock.gettimeout() 41 if timeout != 0.0: 42 try: 43 sock.setblocking(0) 44 return send(sock, data, chunked) 45 finally: 46 sock.setblocking(1) 47 else: 48 return send(sock, data, chunked)
49
50 -def sendlines(sock, lines, chunked=False):
51 for line in list(lines): 52 send(sock, line, chunked)
53
54 -def sendfile(sock, data, chunked=False):
55 if hasattr(data, 'seek'): 56 data.seek(0) 57 58 while True: 59 binarydata = data.read(CHUNK_SIZE) 60 if binarydata == '': break 61 send(sock, binarydata, chunked)
62 63 # Check if running in Jython 64 if 'java' in sys.platform: 65 from javax.net.ssl import TrustManager, X509TrustManager 66 from jarray import array 67 from javax.net.ssl import SSLContext
68 - class TrustAllX509TrustManager(X509TrustManager):
69 '''Define a custom TrustManager which will blindly accept all certificates''' 70
71 - def checkClientTrusted(self, chain, auth):
72 pass
73
74 - def checkServerTrusted(self, chain, auth):
75 pass
76
77 - def getAcceptedIssuers(self):
78 return None
79 # Create a static reference to an SSLContext which will use 80 # our custom TrustManager 81 trust_managers = array([TrustAllX509TrustManager()], TrustManager) 82 TRUST_ALL_CONTEXT = SSLContext.getInstance("SSL") 83 TRUST_ALL_CONTEXT.init(None, trust_managers, None) 84 # Keep a static reference to the JVM's default SSLContext for restoring 85 # at a later time 86 DEFAULT_CONTEXT = SSLContext.getDefault() 87
88 -def trust_all_certificates(f):
89 '''Decorator function that will make it so the context of the decorated method 90 will run with our TrustManager that accepts all certificates''' 91 def wrapped(*args, **kwargs): 92 # Only do this if running under Jython 93 if 'java' in sys.platform: 94 from javax.net.ssl import SSLContext 95 SSLContext.setDefault(TRUST_ALL_CONTEXT) 96 try: 97 res = f(*args, **kwargs) 98 return res 99 finally: 100 SSLContext.setDefault(DEFAULT_CONTEXT) 101 else: 102 return f(*args, **kwargs)
103 return wrapped 104