Skip to content

Commit

Permalink
Add some more notes to self
Browse files Browse the repository at this point in the history
- Add the test script used in python-triogh-928 to decipher Window's port reuse
  semantics
- Note the existence of the 'tree-format' package (found thanks to
  @itamarst)
  • Loading branch information
njsmith committed Apr 25, 2019
1 parent 5806cd8 commit 60312b4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
61 changes: 61 additions & 0 deletions notes-to-self/how-does-windows-so-reuseaddr-work.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# There are some tables here:
# https://web.archive.org/web/20120206195747/https://msdn.microsoft.com/en-us/library/windows/desktop/ms740621(v=vs.85).aspx
# They appear to be wrong.
#
# See https://github.com/python-trio/trio/issues/928 for details and context

import socket
import errno

modes = ["default", "SO_REUSEADDR", "SO_EXCLUSIVEADDRUSE"]
bind_types = ["wildcard", "specific"]

def sock(mode):
s = socket.socket(family=socket.AF_INET)
if mode == "SO_REUSEADDR":
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
elif mode == "SO_EXCLUSIVEADDRUSE":
s.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
return s

def bind(sock, bind_type):
if bind_type == "wildcard":
sock.bind(("0.0.0.0", 12345))
elif bind_type == "specific":
sock.bind(("127.0.0.1", 12345))
else:
assert False

def table_entry(mode1, bind_type1, mode2, bind_type2):
with sock(mode1) as sock1:
bind(sock1, bind_type1)
try:
with sock(mode2) as sock2:
bind(sock2, bind_type2)
except OSError as exc:
if exc.winerror == errno.WSAEADDRINUSE:
return "INUSE"
elif exc.winerror == errno.WSAEACCES:
return "ACCESS"
raise
else:
return "Success"

print("""
second bind
| default | SO_REUSEADDR | SO_EXCLUSIVEADDRUSE
| specific| wildcard| specific| wildcard| specific| wildcard
first bind ------------------------------------------------------------"""
# default | wildcard | INUSE | Success | ACCESS | Success | INUSE | Success
)

for i, mode1 in enumerate(modes):
for j, bind_type1 in enumerate(bind_types):
row = []
for k, mode2 in enumerate(modes):
for l, bind_type2 in enumerate(bind_types):
entry = table_entry(mode1, bind_type1, mode2, bind_type2)
row.append(entry)
#print(mode1, bind_type1, mode2, bind_type2, entry)
print("%19s | %8s | " % (mode1, bind_type1)
+ " | ".join(["%7s" % entry for entry in row]))
4 changes: 4 additions & 0 deletions notes-to-self/print-task-tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# NOTE:
# possibly it would be easier to use https://pypi.org/project/tree-format/
# instead of formatting by hand like this code does...

"""
Demo/exploration of how to print a task tree. Outputs:
Expand Down

0 comments on commit 60312b4

Please sign in to comment.