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   
 8  CHUNK_SIZE = (16 * 1024) 
 9  MAX_BODY = 1024 * 112 
10  DNS_TIMEOUT = 60 
11   
12  try: 
13      import ssl # python 2.6 
14      have_ssl = True 
15  except ImportError: 
16      have_ssl = False 
17   
18    
19  _allowed_ssl_args = ('keyfile', 'certfile', 'server_side', 
20                      'cert_reqs', 'ssl_version', 'ca_certs',  
21                      'do_handshake_on_connect', 'suppress_ragged_eofs') 
22   
23 -def validate_ssl_args(ssl_args):
24 for arg in ssl_args: 25 if arg not in _allowed_ssl_args: 26 raise TypeError('connect() got an unexpected keyword argument %r' % arg)
27
28 -def close(skt):
29 if not skt or not hasattr(skt, "close"): return 30 try: 31 skt.close() 32 except socket.error: 33 pass
34
35 -def send_chunk(sock, data):
36 chunk = "".join(("%X\r\n" % len(data), data, "\r\n")) 37 sock.sendall(chunk)
38
39 -def send(sock, data, chunked=False):
40 if chunked: 41 return send_chunk(sock, data) 42 sock.sendall(data)
43
44 -def send_nonblock(sock, data, chunked=False):
45 timeout = sock.gettimeout() 46 if timeout != 0.0: 47 try: 48 sock.setblocking(0) 49 return send(sock, data, chunked) 50 finally: 51 sock.setblocking(1) 52 else: 53 return send(sock, data, chunked)
54
55 -def sendlines(sock, lines, chunked=False):
56 for line in list(lines): 57 send(sock, line, chunked)
58
59 -def sendfile(sock, data, chunked=False):
60 if hasattr(data, 'seek'): 61 data.seek(0) 62 63 while True: 64 binarydata = data.read(CHUNK_SIZE) 65 if binarydata == '': break 66 send(sock, binarydata, chunked)
67