Last active
December 23, 2020 09:12
-
-
Save Torxed/e6bf902aa86289b197b20607a9714d4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from socket import * | |
sockets = {} | |
s = socket() | |
s.bind(('', 1234)) | |
s.listen() | |
ns, na = s.accept() | |
sockets[na] = ns | |
if na in sockets: | |
sockets[na].close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cheers :) Just a quick remark on that IPv4,
0.0.0.0
is actually not localhost.0.0.0.0
is actually a non-routable IP, used as placeholder for various things.Python automatically parse this nonsense IP as "all available IP's", and most kernels and services do too.
localhost
however (or127.1
for short) is routable, and is an actual interface on most operating systems.So binding to
127.1
will ensure that your socket is only accessible locally, where as0.0.0.0
will let the socket be available to the world and localhost.More on the topic on stackoverflow and on this random blog.
But yes, the rabbit hole is deep - and this is why gists can be a bit confusing for complex topics.
To better and fully understand them, there's many other examples. But this was a mini-example as you mentioned and gists are great for it.
Ps. Yea I was a bit ashamed of my lazy variable naming. Fell back to common annotations for
ns
andna
because that's widely used. But that's no excuse, so re-wrote them heh.