Created
December 26, 2018 08:56
-
-
Save namuyan/308969a599313cd279bfe0d45bc93150 to your computer and use it in GitHub Desktop.
Pythonのipv6のSocketサーバコード
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
import socket | |
import selectors | |
import sys | |
import time | |
HOST = None | |
# HOST = "localhost" # "::1" # "127.0.0.1", "None" | |
PORT = 2000 | |
BACKLOG = 5 | |
BUFSIZE = 1024 | |
sel = selectors.DefaultSelector() | |
def read_server(sock, mask): | |
try: | |
conn, addr = sock.accept() | |
print('Connected by', addr) | |
sel.register(conn, selectors.EVENT_READ, read_client) | |
except: | |
pass | |
try: conn.close() | |
except: pass | |
def read_client(sock, mask): | |
while True: | |
data = sock.recv(BUFSIZE) | |
if not data: break | |
sock.send(data) | |
sock.close() | |
def listen_loop(): | |
while True: | |
while len(sel.get_map()) == 0: | |
time.sleep(0.5) | |
events = sel.select() | |
for key, mask in events: | |
callback = key.data | |
callback(key.fileobj, mask) | |
def main(): | |
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): | |
print(res) | |
af, socktype, proto, canonname, sa = res | |
try: | |
sock = socket.socket(af, socktype, proto) | |
except OSError as msg: | |
print(1, msg) | |
continue | |
try: | |
if af == socket.AF_INET: | |
print("IPV4") | |
elif af == socket.AF_INET6: | |
print("IPV6") | |
sock.bind(sa) | |
sock.listen(BACKLOG) | |
sel.register(sock, selectors.EVENT_READ, read_server) | |
except OSError as msg: | |
sock.close() | |
print(2, msg) | |
continue | |
print(sel.fileno()) | |
if len(sel.get_map()) == 0: | |
print('could not open socket') | |
sys.exit(1) | |
else: | |
listen_loop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment