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