Guess the type of a file. Argument is a PATH (a filename). Return value is a string of the form type/subtype, usable for a MIME Content-type header. The default implementation looks the file's extension up in the table self.extensions_map, using application
(self, path)
| 942 | shutil.copyfileobj(source, outputfile) |
| 943 | |
| 944 | def guess_type(self, path): |
| 945 | """Guess the type of a file. |
| 946 | |
| 947 | Argument is a PATH (a filename). |
| 948 | |
| 949 | Return value is a string of the form type/subtype, |
| 950 | usable for a MIME Content-type header. |
| 951 | |
| 952 | The default implementation looks the file's extension |
| 953 | up in the table self.extensions_map, using application/octet-stream |
| 954 | as a default; however it would be permissible (if |
| 955 | slow) to look inside the data to make a better guess. |
| 956 | |
| 957 | """ |
| 958 | base, ext = posixpath.splitext(path) |
| 959 | if ext in self.extensions_map: |
| 960 | return self.extensions_map[ext] |
| 961 | ext = ext.lower() |
| 962 | if ext in self.extensions_map: |
| 963 | return self.extensions_map[ext] |
| 964 | guess, _ = mimetypes.guess_file_type(path) |
| 965 | if guess: |
| 966 | return guess |
| 967 | return 'application/octet-stream' |
| 968 | |
| 969 | |
| 970 | # Utilities for CGIHTTPRequestHandler |