|
3 | 3 | __all__ = ['dict2obj', 'repr_dict', 'is_listy', 'shufflish', 'mapped', 'IterLen', 'ReindexCollection', 'open_file', |
4 | 4 | 'save_pickle', 'load_pickle', 'maybe_open', 'image_size', 'bunzip', 'join_path_file', 'urlquote', 'urlwrap', |
5 | 5 | 'urlopen', 'urlread', 'urljson', 'urlcheck', 'urlclean', 'urlsave', 'urlvalid', 'untar_dir', 'repo_details', |
6 | | - 'run', 'do_request', 'threaded', 'start_server', 'start_client', 'sort_by_run', 'trace', 'round_multiple', |
7 | | - 'modified_env', 'ContextManagers', 'str2bool', 'set_num_threads', 'ProcessPoolExecutor', |
| 6 | + 'run', 'do_request', 'threaded', 'startthread', 'start_server', 'start_client', 'sort_by_run', 'trace', |
| 7 | + 'round_multiple', 'modified_env', 'ContextManagers', 'str2bool', 'set_num_threads', 'ProcessPoolExecutor', |
8 | 8 | 'ThreadPoolExecutor', 'parallel', 'run_procs', 'parallel_gen'] |
9 | 9 |
|
10 | 10 | # Cell |
@@ -193,25 +193,25 @@ def urlquote(url): |
193 | 193 | return urlunparse(p) |
194 | 194 |
|
195 | 195 | # Cell |
196 | | -def urlwrap(url): |
| 196 | +def urlwrap(url, data=None, headers=None): |
197 | 197 | "Wrap `url` in a urllib `Request` with a user-agent header" |
198 | | - if not isinstance(url,Request): url = Request(urlquote(url)) |
| 198 | + if not isinstance(url,Request): url = Request(urlquote(url), data=data, headers=headers or {}) |
199 | 199 | url.headers['User-Agent'] = _ua |
200 | 200 | return url |
201 | 201 |
|
202 | 202 | # Cell |
203 | | -def urlopen(url, data=None, **kwargs): |
| 203 | +def urlopen(url, data=None, headers=None, **kwargs): |
204 | 204 | "Like `urllib.request.urlopen`, but first `urlwrap` the `url`, and encode `data`" |
205 | 205 | if kwargs and not data: data=kwargs |
206 | 206 | if data is not None: |
207 | 207 | if not isinstance(data, (str,bytes)): data = urlencode(data) |
208 | 208 | if not isinstance(data, bytes): data = data.encode('ascii') |
209 | | - return urllib.request.urlopen(urlwrap(url)) |
| 209 | + return urllib.request.urlopen(urlwrap(url, data=data, headers=headers)) |
210 | 210 |
|
211 | 211 | # Cell |
212 | | -def urlread(url, data=None, **kwargs): |
| 212 | +def urlread(url, data=None, headers=None, **kwargs): |
213 | 213 | "Retrieve `url`, using `data` dict or `kwargs` to `POST` if present" |
214 | | - with urlopen(url, data=data) as res: return res.read() |
| 214 | + with urlopen(url, data=data, headers=headers, **kwargs) as res: return res.read() |
215 | 215 |
|
216 | 216 | # Cell |
217 | 217 | def urljson(url, data=None): |
@@ -292,6 +292,11 @@ def _f(*args, **kwargs): |
292 | 292 | return res |
293 | 293 | return _f |
294 | 294 |
|
| 295 | +# Cell |
| 296 | +def startthread(f): |
| 297 | + "Like `threaded`, but start thread immediately" |
| 298 | + threaded(f)() |
| 299 | + |
295 | 300 | # Cell |
296 | 301 | def _socket_det(port,host,dgram): |
297 | 302 | if isinstance(port,int): family,addr = socket.AF_INET,(host or socket.gethostname(),port) |
|
0 commit comments