Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python for web architectures

Shuhei Ozawa
February 09, 2018
830

Python for web architectures

Python Programming for web architectures

Shuhei Ozawa

February 09, 2018
Tweet

Transcript

  1. γϦΞϧϞσϧ — socket(),bind(),listen()Ͱιέοτͷ࡞੒ def create_listen_socket(host, port): """ αʔόʔ͕઀ଓཁٻΛड͚औΔιέοτΛઃఆ͢Δ """ #

    ΞυϨεϑΝϛϦʔɺιέοτλΠϓɺϓϩτίϧ൪߸Λࢦఆͯ͠৽͍͠ιέοτΛ࡞੒ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((host, port)) sock.listen(100) return sock ٢঵ࣉ.pm13 2018/02/09 - Ozawa Shuhei ( @oza_shu ) 9
  2. — ϝοηʔδΛsocketʹ઀ଓͯ͠ૹ৴ if __name__ == '__main__': while True: try: sock

    = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((HOST, PORT)) print('\nConnected to {}:{}'.format(HOST, PORT)) print("Type message, enter to send, 'q' to quit") msg = input() if msg == 'q': break chatmodule.send_msg(sock, msg) # ૹ৴͢Δ·ͰBlock print('Sent message: {}'.format(msg)) msg = chatmodule.recv_msg(sock) # messageΛ׬શʹड৴͢Δ·ͰBlock print('Received echo: ' + msg) except ConnectionError: print('Socket error') break finally: sock.close() print('Closed connection to server\n') ٢঵ࣉ.pm13 2018/02/09 - Ozawa Shuhei ( @oza_shu ) 10
  3. — ϝοηʔδΛsocket͔Βड৴ def handle_client(sock, addr): """ sockΛ௨ͯ͡client͔ΒσʔλΛड͚ͱΓ,echoΛฦ͢ """ try: msg

    = chatmodule.recv_msg(sock) # messageΛ׬શʹड৴͢Δ·Ͱblock print('{}: {}'.format(addr, msg)) chatmodule.send_msg(sock, msg) # ૹ৴͢Δ·Ͱblock except (ConnectionError, BrokenPipeError): print('Socket error') finally: print('Closed connection to {}'.format(addr)) sock.close() if __name__ == '__main__': listen_sock = chatmodule.create_listen_socket(HOST, PORT) # ιέοτࣗ਎ͷΞυϨεΛฦ͢ # ͜ͷؔ਺͸ɺIPv4/v6ιέοτͷϙʔτ൪߸Λௐ΂Δ৔߹ͳͲʹ࢖༻ɻ addr = listen_sock.getsockname() print('Listening on {}'.format(addr)) while True: client_sock, addr = listen_sock.accept() print('Connection from {}'.format(addr)) handle_client(client_sock, addr) ٢঵ࣉ.pm13 2018/02/09 - Ozawa Shuhei ( @oza_shu ) 11
  4. ϚϧνϓϩηεϞσϧ — fork֤ͤͯ͞ϓϩηε͕֤ΫϥΠΞϯτͱ௨৴Ͱ͖Δ — workerϞσϧ — accept()ͨ͋͠ͱͷॲཧΛϓϩηεʹॲཧͤ͞Δ — preforkϞσϧ —

    accept()͔Βclose()·ͰͷॲཧΛϓϩηεʹॲཧͤ͞Δ ٢঵ࣉ.pm13 2018/02/09 - Ozawa Shuhei ( @oza_shu ) 13
  5. ίʔυྫ workerϞσϧ while True: client_sock,addr = listen_sock.accept() proc = Process(target=handle_client,

    args=[client_sock, addr]) proc.start() print('Connection from {}'.format(addr)) proc.join(1) ٢঵ࣉ.pm13 2018/02/09 - Ozawa Shuhei ( @oza_shu ) 14
  6. ϚϧνεϨουϞσϧ جຊతʹ͸Ϛϧνϓϩηε/εϨου͸ಉ͡ - 1ίωΫγϣϯ1εϨουͷϞσϧ - ϦΫΤετ͝ͱʹεϨουΛੜ੒ - εϨουϓʔϧ - ࣄલʹεϨουΛPool͓ͯ͘͠Ϟσϧ

    εϨου͸ϝϞϦڞ༗͍ͯ͠ΔͷͰɺεϨουηʔϑͳ࣮૷͕ ඞཁʹͳΔ (Ωϡʔ΍ϩοΫͷॲཧ͸ׂѪ) ٢঵ࣉ.pm13 2018/02/09 - Ozawa Shuhei ( @oza_shu ) 16
  7. ίʔυྫ 1ίωΫγϣϯ1εϨουͷϞσϧ if __name__ == '__main__': listen_sock = chatmodule.create_listen_socket(HOST, PORT)

    addr = listen_sock.getsockname() print('Listening on {}'.format(addr)) while True: client_sock,addr = listen_sock.accept() # Thread ͸ࣗಈతʹhandle_client()ؔ਺Λ࣮ߦ͠ɺಉ࣌ʹ͜ͷwhile loopΛ࣮ߦ thread = threading.Thread(target=handle_client, args=[client_sock, addr], daemon=True) thread.start() print('Connection from {}'.format(addr)) ٢঵ࣉ.pm13 2018/02/09 - Ozawa Shuhei ( @oza_shu ) 17