Cédric Bonhomme avatar Cédric Bonhomme committed d827111

UTF-8 charset

Comments (0)

Files changed (1)

 errors = []
 
 html = '<html>\n' + \
-        '<head>\n\t<meta charset="utf-8" />\n\t<title>Find a Bike</title>\n\t' + \
+        '<head>\n\t<meta http-equiv="content-type" content="text/html; charset=utf-8" />\n\t<title>Find a Bike</title>\n\t' + \
         '<link rel="stylesheet" href="./style.css" />\n'
 
 try:
     if city == "":
         try:
             request = urllib.request.urlopen(url)
-            text = request.read()
-            stations = json.loads(text.decode())
-        except:
-            pass
+            text = request.read().decode("utf-8")
+            stations = json.loads(text)
+        except Exception as e:
+            print(e)
     else:
         cities = city.split(',')
         for current_city in cities:
         if station["available_bikes"] == 0:
             html += """
                         popupContentHTML = "<p><b>%s</b><hr />No bikes available.<br />Payment terminal: %s<br />Last update: %s</p>";""" % \
-                            (station["name"], [station["banking"]==True and "yes" or "no"][0], \
+                            (cgi.escape(station["name"], quote=True).encode("ascii", "xmlcharrefreplace").decode(), [station["banking"]==True and "yes" or "no"][0], \
                                 datetime.datetime.fromtimestamp(station['last_update']/1000).strftime('%Y-%m-%d %H:%M:%S'))
             html += """
                         addMarker(station%s, popupClass, popupContentHTML, true, true, "./marker-red.png", stations_location);""" % \
         else:
             html += """
                         popupContentHTML = "<p><b>%s</b><hr />Available bikes: %s<br />Payment terminal: %s<br />Last update: %s</p>";""" % \
-                            (station["name"], station["available_bikes"], [station["banking"]==True and "yes" or "no"][0], \
+		    (cgi.escape(station["name"], quote=True).encode("ascii", "xmlcharrefreplace").decode(), station["available_bikes"], [station["banking"]==True and "yes" or "no"][0], \
                                 datetime.datetime.fromtimestamp(station['last_update']/1000).strftime('%Y-%m-%d %H:%M:%S'))
             html += """
                         addMarker(station%s, popupClass, popupContentHTML, true, true, "./marker-green.png", stations_location);""" % \
 
 
 # Finally print the web page.
-print("Content-type:text/html\r\n\r\n")
+print("Content-type:text/html; Accept-Charset: utf-8")
+print("")
 print(html)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.