6 ms·
Every time I've written something that uses a port I've added a check to see if the code returns an EADDRINUSE error when the listener connects, and offered the
by onion2k 5y ago
Every time I've written something that uses a port I've added a check to see if the code returns an EADDRINUSE error when the listener connects, and offered the user an alternative port if it does. I always assumed everyone did that but apparently not. Maybe I should make an NPM package...
- zinekeller 5y agoIt should be a Pip package instead! (The main non-IANA compliant user of the port is Flask, a Python-based web framework.)
- onion2k 5y agoNo one would want to use a Python package written by a JS developer. :)
- emteycz 5y agoNo one would see any difference, any perceived difference in quality is a meme
- OJFord 5y agoI don't know, if you'd never done any Python, and perhaps particularly if you learnt JS in 'callback hell' days, you might not realise what was wrong with: def doSomething(theArgs, cb): food, bard = 0, 0 try: doFoo(theArgs["foo"], lambda: food = 1) doBar(theArgs["bar"], lambda: bard = 1) except Exception: cb(food + bard, "didn't work") else: cb(food + bard, None) or whatever. I can get by in JS, I use it professionally even now, but it's not good quality; I know that. (Trivial example, I discovered `??` recently. I've definitely written a lot of null/undefined checks without it, or even naive ternaries that assume non-null/undef values aren't falsey (`thing ? thing : 123` vs. `thing ?? 123`) - which is kind of like seeing `dict.get(key, None)` in python.)
- spiffytech 5y agoFun trick I learned: binding to port 0 causes Linux to pick a random, unused, ephemeral port for you. That's what I've started using in my EADDRINUSE fallback.
- alvarlagerlof 5y agoNice
- Joker_vD 5y ago...it boggles my mind that listening on port 0 and then reporting the actual bound port is not the default. It takes only a couple minutes more to properly write (and another couple of minutes to write the code to properly consume the reported port) but it offers tremendous convenience.
- jorams 5y agoIn the case of Flask: It would be incredibly inconvenient to restart your development server and have it show up on a random port every single time. The only time fixed port numbers are inconvenient is when they conflict, but otherwise they're much easier to work with.
- Joker_vD 5y agoYes, "worse is better", the ultimate reason of why inferior technical decisions get universally adopted :/ Of course, one day the technical inferiority finally shows itself (e.g., the ports do conflict), and then people start to heroically overcome the obstacles they've created for themselves, showing outstanding feats of ingenuity. Just google "port allocator"! Every company inevitably writes their own home-grown one because everyone's deployment/integration pipeline is unique.
- macintux 5y agoYou could always launch once using that scheme, stash the port that you’re offered, and re-use that until it stops working. It’s likely that the port will be available indefinitely.