Use "data" URL.
(self, url, data=None)
| 2095 | raise URLError(f'ftp error: {exp}') from exp |
| 2096 | |
| 2097 | def open_data(self, url, data=None): |
| 2098 | """Use "data" URL.""" |
| 2099 | if not isinstance(url, str): |
| 2100 | raise URLError('data error: proxy support for data protocol currently not implemented') |
| 2101 | # ignore POSTed data |
| 2102 | # |
| 2103 | # syntax of data URLs: |
| 2104 | # dataurl := "data:" [ mediatype ] [ ";base64" ] "," data |
| 2105 | # mediatype := [ type "/" subtype ] *( ";" parameter ) |
| 2106 | # data := *urlchar |
| 2107 | # parameter := attribute "=" value |
| 2108 | try: |
| 2109 | [type, data] = url.split(',', 1) |
| 2110 | except ValueError: |
| 2111 | raise OSError('data error', 'bad data URL') |
| 2112 | if not type: |
| 2113 | type = 'text/plain;charset=US-ASCII' |
| 2114 | semi = type.rfind(';') |
| 2115 | if semi >= 0 and '=' not in type[semi:]: |
| 2116 | encoding = type[semi+1:] |
| 2117 | type = type[:semi] |
| 2118 | else: |
| 2119 | encoding = '' |
| 2120 | msg = [] |
| 2121 | msg.append('Date: %s'%time.strftime('%a, %d %b %Y %H:%M:%S GMT', |
| 2122 | time.gmtime(time.time()))) |
| 2123 | msg.append('Content-type: %s' % type) |
| 2124 | if encoding == 'base64': |
| 2125 | # XXX is this encoding/decoding ok? |
| 2126 | data = base64.decodebytes(data.encode('ascii')).decode('latin-1') |
| 2127 | else: |
| 2128 | data = unquote(data) |
| 2129 | msg.append('Content-Length: %d' % len(data)) |
| 2130 | msg.append('') |
| 2131 | msg.append(data) |
| 2132 | msg = '\n'.join(msg) |
| 2133 | headers = email.message_from_string(msg) |
| 2134 | f = io.StringIO(msg) |
| 2135 | #f.fileno = None # needed for addinfourl |
| 2136 | return addinfourl(f, headers, url) |
| 2137 | |
| 2138 | |
| 2139 | class FancyURLopener(URLopener): |