-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpywwwget_clean_tcpudp.py
More file actions
142 lines (125 loc) · 3.74 KB
/
pywwwget_clean_tcpudp.py
File metadata and controls
142 lines (125 loc) · 3.74 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pywwwget_clean_tcpudp.py
Cleaned, optimized, Python 2/3 compatible TCP/UDP transfer helper.
Keeps the SAME API style as pywwwget.
SUPPORTED SCHEMES:
tcp, udp
PUBLIC API:
download_file_from_internet_file(url)
download_file_from_internet_string(url)
upload_file_to_internet_file(fileobj, url)
upload_file_to_internet_string(data, url)
QUERY OPTIONS (?key=value):
timeout=<sec>
total_timeout=<sec>
chunk=<bytes>
max_bytes=<bytes>
rate=<bytes/sec>
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import socket, time, tempfile, logging
try:
from urllib.parse import urlparse, parse_qs
except Exception:
from urlparse import urlparse, parse_qs
try:
from io import BytesIO
except Exception:
from StringIO import StringIO as BytesIO
log = logging.getLogger("pywwwget_clean")
if not log.handlers:
logging.basicConfig(level=logging.INFO)
DEFAULT_CHUNK = 65536
def MkTempFile():
try:
return tempfile.SpooledTemporaryFile(max_size=8*1024*1024, mode="w+b")
except Exception:
return BytesIO()
def _parse_opts(url):
p = urlparse(url)
q = parse_qs(p.query or "")
def gi(name, default):
try:
return int(q.get(name, [default])[0])
except Exception:
return default
return {
"timeout": gi("timeout", 10),
"total_timeout": gi("total_timeout", 0),
"chunk": gi("chunk", DEFAULT_CHUNK),
"max_bytes": gi("max_bytes", 0),
"rate": gi("rate", 0),
}
def send_from_fileobj(sock, f, opts):
sent = 0
start = time.time()
while True:
data = f.read(opts["chunk"])
if not data:
break
sock.sendall(data)
sent += len(data)
if opts["rate"]:
elapsed = time.time() - start
expected = sent / float(opts["rate"])
if expected > elapsed:
time.sleep(expected - elapsed)
return sent
def recv_to_fileobj(sock, f, opts):
total = 0
start = time.time()
while True:
if opts["total_timeout"] and time.time() - start > opts["total_timeout"]:
break
data = sock.recv(opts["chunk"])
if not data:
break
total += len(data)
if opts["max_bytes"] and total > opts["max_bytes"]:
raise IOError("max_bytes exceeded")
f.write(data)
return total
def upload_file_to_internet_file(fileobj, url):
p = urlparse(url)
opts = _parse_opts(url)
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM if p.scheme == "tcp" else socket.SOCK_DGRAM)
sock.settimeout(opts["timeout"])
sock.connect((p.hostname or "0.0.0.0", p.port or 0))
try:
try:
fileobj.seek(0)
except Exception:
pass
return send_from_fileobj(sock, fileobj, opts)
finally:
sock.close()
def download_file_from_internet_file(url):
p = urlparse(url)
opts = _parse_opts(url)
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM if p.scheme == "tcp" else socket.SOCK_DGRAM)
sock.settimeout(opts["timeout"])
sock.bind((p.hostname or "0.0.0.0", p.port or 0))
if p.scheme == "tcp":
sock.listen(1)
conn, _ = sock.accept()
else:
conn = sock
out = MkTempFile()
try:
recv_to_fileobj(conn, out, opts)
out.seek(0)
return out
finally:
try:
conn.close()
except Exception:
pass
sock.close()
def download_file_from_internet_string(url):
return download_file_from_internet_file(url).read()
def upload_file_to_internet_string(data, url):
return upload_file_to_internet_file(BytesIO(data), url)