forked from jchris/couchapp
/
sock.py
59 lines (48 loc) · 1.58 KB
/
sock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -
#
# This file is part of restkit released under the MIT license.
# See the NOTICE for more information.
import socket
CHUNK_SIZE = (16 * 1024)
MAX_BODY = 1024 * 112
DNS_TIMEOUT = 60
_allowed_ssl_args = ('keyfile', 'certfile', 'server_side',
'cert_reqs', 'ssl_version', 'ca_certs',
'do_handshake_on_connect', 'suppress_ragged_eofs')
def validate_ssl_args(ssl_args):
for arg in ssl_args:
if arg not in _allowed_ssl_args:
raise TypeError('connect() got an unexpected keyword argument %r' % arg)
def close(skt):
if not skt or not hasattr(skt, "close"): return
try:
skt.close()
except socket.error:
pass
def send_chunk(sock, data):
chunk = "".join(("%X\r\n" % len(data), data, "\r\n"))
sock.sendall(chunk)
def send(sock, data, chunked=False):
if chunked:
return send_chunk(sock, data)
sock.sendall(data)
def send_nonblock(sock, data, chunked=False):
timeout = sock.gettimeout()
if timeout != 0.0:
try:
sock.setblocking(0)
return send(sock, data, chunked)
finally:
sock.setblocking(1)
else:
return send(sock, data, chunked)
def sendlines(sock, lines, chunked=False):
for line in list(lines):
send(sock, line, chunked)
def sendfile(sock, data, chunked=False):
if hasattr(data, 'seek'):
data.seek(0)
while True:
binarydata = data.read(CHUNK_SIZE)
if binarydata == '': break
send(sock, binarydata, chunked)