Unfortunately, python 2.7 doesn’t support SNI much to my regret. Thanks to an HN comment I was pointed to a python issue. The problem has been known about for five years, but fixing things isn’t the python way. Finally, somebody saw the light which led to PEP 466. Current status: partially implemented.
Where does this leave me? I could upgrade to python 3.4, but none of the auxiliary libraries I need (notably py-feedparser) are available as OpenBSD packages except for versions built against 2.7. Or I can wait for python 2.7.9, although as a practical matter that would also mean upgrading OpenBSD and everything else (and likely not until May) so maybe I’d rather not. And that’s if 2.7.9 actually includes working SNI support. Digging through the issue tracker, it sounds like only optional support will be included, and programs will need to be changed and updated as well. It’s very important that upgrades don’t make things work by accident.
There is also the inject_into_urllib3 approach which I’m honestly kind of scared of, but it could work.
Instead my solution was to change the Duo blog’s URL to a file on disk, fetched by ftp running out of cron.
So here’s pyggy. It’s really simple. It listens on a port. You feed it a language, a length, and some code. It returns a length and some highlighted html. It’s not configurable. Maybe someone will find it useful.
import socket import pygments from pygments import formatters,lexers html = formatters.get_formatter_by_name("html", noclasses=True, nowrap=True) getlexer = lexers.get_lexer_by_name highlight = pygments.highlight serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serv.bind(('127.0.0.1', 7878)) serv.listen(10) while True: con, addr = serv.accept() try: fd = con.makefile() lang = fd.readline().strip() amt = int(fd.readline()) code = fd.read(amt) rv = "" try: lex = getlexer(lang) rv = highlight(code, lex, html) except: pass fd.write(str(len(rv))) fd.write("\n") fd.write(rv) fd.close() except: pass con.close()