flak rss random

python 3k17

New year, time for a new python, right? I’ve been sticking for python2 but two related events led me to try python3. The first was python3.6, which has a bunch of new features, notably finalized async support. No plans to actually use said support myself, but it seems like the kind of landmark feature that will convince other people to switch, so I figured I would hop on board. The second thing was python3.6 being available as an OpenBSD package. The scene was set for a day spent updating code. If you don’t use python, this will probably not be of much interest.

more...

Posted 05 Jan 2017 17:30 by tedu Updated: 05 Jan 2017 17:30
Tagged: python

the trouble with python and SNI

Server Name Indication is a TLS extension that allows the client to tell the server what hostname it would like to talk to. It solves, in theory, one of the issues with moving a web server with many virtual hosts to https: different hostnames need different certs.

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.

Posted 16 Nov 2014 07:28 by tedu Updated: 05 Dec 2014 03:12
Tagged: python rants software

pyggy the simple pygments server

flak uses pygments to generate the pretty highlighted code blocks. The original approach was to just shell out to the command line tool, but the performance is, ah, suboptimal. Caching mostly saves me, but at 0.1 secs per execution, highlighting five small samples pushes the initial page render up to half a second. Searching the intertubes for a pygments server didn’t reveal much. (The top hit is a random sample of highlighted code, not a highligher itself, and the other hits don’t seem very useful either.)

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()

pyggy.py

Posted 03 May 2013 14:57 by tedu Updated: 10 Oct 2014 00:38
Tagged: flak programming python web