0

I am trying to make my own very lightweight webserver in Python 3, and I am running into a problem of serving the correct MIME type. Are there any good ways to identify the MIME type of a string read from a file (not a file itself), preferably without using dependencies such as Magic. I have tried How to find the mime type of a file in python? but this requires installing dependencies which I'd like to avoid.

clubby789
  • 2,113
  • 3
  • 13
  • 24
  • 2
    The whole point of the need to specify the mime type is because it is additional information, which cannot be accurately determined from the contents (otherwise the browser would detect it itself). In other words, you will be better off if you design a server which knows the mime type, rather than trying to detect it. – zvone Sep 21 '19 at 17:48
  • Right now, I'm mainly trying to differentiate between .txt files and HTML files.How could I do what you suggest? – clubby789 Sep 21 '19 at 17:49
  • 1
    Well, you must be reading the contents of the files (i.e. the strings) from somewhere. You should keep the mime type there as well. If it is a file, you can use the extension. If it is a database, it can have a "mimetype" column next to the contents column. If it is something else, then something else. You haven't explained where the data comes from, so I can't say more. – zvone Sep 21 '19 at 17:52
  • Thanks, I'll see what I can do – clubby789 Sep 21 '19 at 18:06

0 Answers0