Live data from Hacker News

CLI tools hidden in the Python standard library

til.simonwillison.net

71–80 of 160 posts

Re: CLI tools hidden in the Python standard library

#71
post #65
post #48

Earlier quoted context omitted.

Wait, how does that work? As far as I can see from the documentation, it can only serve on localhost, which to my understanding is only accessible from the single device it was launched on.

I think you must have misread the documentation [1], which says: "By default, the server binds itself to all interfaces." [1] https://docs.python.org/3/library/http.server.html

Not ‘misread’, just ‘missed that bit entirely’. Testing it out, this is indeed the case.

Re: CLI tools hidden in the Python standard library

#72
post #22

Speaking of hidden Python tools, I'm a big fan of re.Scanner[0]. It's a regex-based tokenizer[1] in the `re` module, that for reasons is completely missing from any official documentation. You give it a pattern for each token type, and a function to be called on each match, and you get back a list of processed tokens. Importantly, it processes the list in one pass and ensures the matches are contiguous, where a naive…

Amazing! I had just written a regex tokenizer in python the other day, this would have been great!

Re: CLI tools hidden in the Python standard library

#73
post #10

There is one problem with those: Security. Using modules (even if they are in the standard library) on the command line lets malicious code in the current dir take over your machine: https://twitter.com/marekgibney/status/1598706464583028736

I find the entire premise of the post to be pretty baffling.

> Seth pointed out this is useful if you are on Windows and don't have the gzip utility installed.

Okay, so instead of installing gzip (or just using the decompressors that aren't the official gzip utility but that do support the format and already ship with Windows by default[1]), you install Python...?

Even if the premise weren't muddy from the start, there is a language runtime and ubiquitous cross-platform API already available on all major desktops that has a really good, industrial strength sandbox/containerization strategy that more than adequately mitigates the security issues raised here so you can e.g. rot13 without fear all day to your delight: the browser.

1. https://news.ycombinator.com/item?id=36099528>

Re: CLI tools hidden in the Python standard library

#74

> I thought this might provide a utility for generating random numbers, but sadly it's just a benchmarking suite with no additional command-line options: I had the same experience with the tkinter ones - I thought they might be like zenity, a way to build simple UI elements from the command line. But they mostly just show simple non-configurable test widgets. The colour chooser could be helpful though.

The cli tool [fire](https://github.com/google/python-fire/blob/master/docs/guide...) has a nifty feature where it can generate a cli for any file for you.

So random and math are somewhat usable that way

    $ python -m fire random uniform 0 1
    0.5502786602920726

    $ python -m fire math radians 180
    3.141592653589793

    $ python -m fire math e
    2.718281828459045
Just running

    $ python -m fire random
will give you a nice "manpage" for your module as well.

Re: CLI tools hidden in the Python standard library

#75
post #73
post #10

There is one problem with those: Security. Using modules (even if they are in the standard library) on the command line lets malicious code in the current dir take over your machine: https://twitter.com/marekgibney/status/1598706464583028736

I find the entire premise of the post to be pretty baffling. > Seth pointed out this is useful if you are on Windows and don't have the gzip utility installed. Okay, so instead of installing gzip (or just using the decompressors that aren't the official gzip utility but that do support the format and already ship with Windows by default[1]), you install Python...? Even if the premise weren't muddy from the start, the…

Most people who read this blog already have python installed

Re: CLI tools hidden in the Python standard library

#77
post #10

There is one problem with those: Security. Using modules (even if they are in the standard library) on the command line lets malicious code in the current dir take over your machine: https://twitter.com/marekgibney/status/1598706464583028736

If running with -m, or -c and an import of package matching name of malicious package. Doesn't happen when running your own script (located in another directory) that imports that package, even if you are running it in that directory.

Re: CLI tools hidden in the Python standard library

#78
post #10

There is one problem with those: Security. Using modules (even if they are in the standard library) on the command line lets malicious code in the current dir take over your machine: https://twitter.com/marekgibney/status/1598706464583028736

iirc this is one of the things earmarked for a hypothetical Python 4, making -P the default. It's also one of the many relatively well-known (security) issues in Python that don't get addressed for a surprising amount of time. Others in the same vein would be stuff like stderr being block-buffered when not using a TTY, no randomized hashes for the longest time, loading DLLs preferably from the working directory, std*…

-P?

Re: CLI tools hidden in the Python standard library

#79
post #67

I use http.server all the time, particularly as modern browsers disable a bunch of functionality if you open file URLs. Had no idea there was so much other stuff here!

I use miniserve (`cargo install miniserve`). You also have available `npx serve`.

Oh and the Rust brigade have arrived... Was only a matter of time.

Re: CLI tools hidden in the Python standard library

#80
post #63
post #39

Earlier quoted context omitted.

Especially now that they are on a crusade against their own stdlib. They regret including most modules… it seems they regret making python altogether instead of sticking with C? :D

> They regret including most modules… it seems they regret making python altogether instead of sticking with C? :D I also find it odd. Python would probably be a little known language without the huge batteries included by default. It's invaluable when you're working in a environment where you can't fully control what is installed, what is the case of most people at work. I believe this crusade endangers the language…

You're reading a ridiculous mischaracterization of reality (~20 deprecated modules [1] out of the ~300 PSL modules [2] is "most"?). Of course you find it odd.

[1] https://peps.python.org/pep-0594/

[2] https://docs.python.org/3/library/

Post reply on HN