{"id":11766,"date":"2026-07-03T11:39:42","date_gmt":"2026-07-03T02:39:42","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=11766"},"modified":"2026-07-03T11:44:24","modified_gmt":"2026-07-03T02:44:24","slug":"python-select-select","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=11766","title":{"rendered":"python select.select()"},"content":{"rendered":"<h1>python select.select()<\/h1>\n<h2>\uae30\ub2a5<\/h2>\n<p>\ub2e4\uc218\uc758 \uc77d\uae30\/\uc4f0\uae30\/\uc608\uc678 \ub370\uc774\ud0c0 \ucc98\ub9ac\ud558\ub294 \uac83\uc744 \ubaa8\ub2c8\ud130\ub9c1 \ud569\ub2c8\ub2e4.<\/p>\n<h2>\uc7a5\ub2e8\uc810<\/h2>\n<p>\uc774\uc2dd\uc131\uc774 \uc88b\ub2e4.<\/p>\n<p>1000\uac1c \uc774\uc0c1\uc758 \ub9ac\uc2a4\ud2b8\ub97c \ucc98\ub9ac\ud558\ub824\uace0 \ud558\uba74 \uc131\ub2a5\uc774\uc288\uac00 \ubc1c\uc0dd\ud55c\ub2e4.<\/p>\n<h2>\uc11c\ubc84 \uc608\uc81c<\/h2>\n<pre><code class=\"language-python\">import select\nimport socket\n\nserver_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nserver_socket.bind((&#039;localhost&#039;, 8080))\nserver_socket.listen()\n\n# \uac10\uc2dc\ud560 \uc18c\ucf13 \ubaa9\ub85d (\ucc98\uc74c\uc5d0\ub294 \uc11c\ubc84 \uc18c\ucf13\ub9cc \ub4f1\ub85d)\nsockets_list = [server_socket]\n\nprint(&quot;\uc11c\ubc84\uac00 \uc2dc\uc791\ub418\uc5c8\uc2b5\ub2c8\ub2e4...&quot;)\n\nwhile True:\n    # sockets_list\uc5d0 \uc788\ub294 \uc18c\ucf13\ub4e4 \uc911 \uc77d\uae30(\uc774\ubca4\ud2b8 \ubc1c\uc0dd) \uc900\ube44\uac00 \ub41c \uc18c\ucf13\uc744 \ucc3e\uc544\uc634\n    read_sockets, _, _ = select.select(sockets_list, [], [])\n\n    for notified_socket in read_sockets:\n        # Case A: \ub9cc\uc57d \uc11c\ubc84 \uc18c\ucf13\uc5d0 \uc774\ubca4\ud2b8\uac00 \uc654\ub2e4\uba74 -&gt; \uc0c8\ub85c\uc6b4 \ud074\ub77c\uc774\uc5b8\ud2b8\uc758 \uc811\uc18d \uc694\uccad\n        if notified_socket == server_socket:\n            client_socket, client_address = server_socket.accept()\n            sockets_list.append(client_socket)  # \uc0c8 \ud074\ub77c\uc774\uc5b8\ud2b8\ub97c \uac10\uc2dc \ubaa9\ub85d\uc5d0 \ucd94\uac00\n            print(f&quot;\uc0c8\ub85c\uc6b4 \uc5f0\uacb0: {client_address}&quot;)\n\n        # Case B: \uae30\uc874 \ud074\ub77c\uc774\uc5b8\ud2b8 \uc18c\ucf13\uc5d0 \uc774\ubca4\ud2b8\uac00 \uc654\ub2e4\uba74 -&gt; \ub370\uc774\ud130\uac00 \ub3c4\ucc29\ud568\n        else:\n            data = notified_socket.recv(1024)\n            if data:\n                notified_socket.send(data)  # \ubc1b\uc740 \ub370\uc774\ud130 \uadf8\ub300\ub85c \uc5d0\ucf54(\uc751\ub2f5)\n            else:\n                # \ub370\uc774\ud130\uac00 \uc5c6\ub2e4\uba74 \ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc5f0\uacb0\uc744 \ub04a\uc740 \uac83\n                sockets_list.remove(notified_socket)\n                notified_socket.close()<\/code><\/pre>\n<h2>\ud074\ub77c\uc774\uc5b8\ud2b8 \uc608\uc81c<\/h2>\n<pre><code class=\"language-python\">import socket\n\n# \uc11c\ubc84\uac00 \uc2e4\ud589 \uc911\uc778 \ud638\uc2a4\ud2b8\uc640 \ud3ec\ud2b8 \ubc88\ud638 \uc9c0\uc815\nSERVER_HOST = &#039;localhost&#039;\nSERVER_PORT = 8080\n\n# 1. \uc18c\ucf13 \uc0dd\uc131 (TCP\/IP)\nclient_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\ntry:\n    # 2. \uc11c\ubc84\uc5d0 \uc5f0\uacb0 \uc694\uccad\n    client_socket.connect((SERVER_HOST, SERVER_PORT))\n    print(f&quot;\uc11c\ubc84({SERVER_HOST}:{SERVER_PORT})\uc5d0 \uc5f0\uacb0\ub418\uc5c8\uc2b5\ub2c8\ub2e4.&quot;)\n    print(&quot;\uc885\ub8cc\ud558\ub824\uba74 &#039;exit&#039;\ub97c \uc785\ub825\ud558\uc138\uc694.\\n&quot;)\n\n    while True:\n        # 3. \uc0ac\uc6a9\uc790\ub85c\ubd80\ud130 \ubcf4\ub0bc \uba54\uc2dc\uc9c0 \uc785\ub825 \ubc1b\uae30\n        message = input(&quot;\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \uc785\ub825: &quot;)\n\n        # &#039;exit&#039; \uc785\ub825 \uc2dc \ub8e8\ud504 \uc885\ub8cc\n        if message.lower() == &#039;exit&#039;:\n            print(&quot;\uc5f0\uacb0\uc744 \uc885\ub8cc\ud569\ub2c8\ub2e4.&quot;)\n            break\n\n        if not message:\n            continue\n\n        # 4. \uc11c\ubc84\ub85c \ub370\uc774\ud130 \uc804\uc1a1 (\ubb38\uc790\uc5f4\uc744 \ubc14\uc774\ud2b8\ub85c \uc778\ucf54\ub529)\n        client_socket.sendall(message.encode(&#039;utf-8&#039;))\n\n        # 5. \uc11c\ubc84\ub85c\ubd80\ud130 \uc751\ub2f5 \ub370\uc774\ud130 \uc218\uc2e0\n        data = client_socket.recv(1024)\n        if not data:\n            print(&quot;\uc11c\ubc84\uc640 \uc5f0\uacb0\uc774 \ub04a\uc5b4\uc84c\uc2b5\ub2c8\ub2e4.&quot;)\n            break\n\n        # \uc218\uc2e0\ud55c \ubc14\uc774\ud2b8 \ub370\uc774\ud130\ub97c \ubb38\uc790\uc5f4\ub85c \ub514\ucf54\ub529\ud558\uc5ec \ucd9c\ub825\n        print(f&quot;\uc11c\ubc84\ub85c\ubd80\ud130 \ubc1b\uc740 \uc751\ub2f5: {data.decode(&#039;utf-8&#039;)}\\n&quot;)\n\nexcept ConnectionRefusedError:\n    print(&quot;\uc11c\ubc84\uc5d0 \uc5f0\uacb0\ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4. \uc11c\ubc84\uac00 \ucf1c\uc838 \uc788\ub294\uc9c0 \ud655\uc778\ud558\uc138\uc694.&quot;)\n\nfinally:\n    # 6. \uc18c\ucf13 \ub2eb\uae30\n    client_socket.close()\n    print(&quot;\uc18c\ucf13\uc774 \ub2eb\ud614\uc2b5\ub2c8\ub2e4.&quot;)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>python select.select() \uae30\ub2a5 \ub2e4\uc218\uc758 \uc77d\uae30\/\uc4f0\uae30\/\uc608\uc678 \ub370\uc774\ud0c0 \ucc98\ub9ac\ud558\ub294 \uac83\uc744 \ubaa8\ub2c8\ud130\ub9c1 \ud569\ub2c8\ub2e4. \uc7a5\ub2e8\uc810 \uc774\uc2dd\uc131\uc774 \uc88b\ub2e4. 1000\uac1c \uc774\uc0c1\uc758 \ub9ac\uc2a4\ud2b8\ub97c \ucc98\ub9ac\ud558\ub824\uace0 \ud558\uba74 \uc131\ub2a5\uc774\uc288\uac00 \ubc1c\uc0dd\ud55c\ub2e4. \uc11c\ubc84 \uc608\uc81c import select import socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind((&#039;localhost&#039;, 8080)) server_socket.listen() # \uac10\uc2dc\ud560 \uc18c\ucf13 \ubaa9\ub85d (\ucc98\uc74c\uc5d0\ub294 \uc11c\ubc84 \uc18c\ucf13\ub9cc \ub4f1\ub85d) sockets_list = [server_socket] print(&quot;\uc11c\ubc84\uac00 \uc2dc\uc791\ub418\uc5c8\uc2b5\ub2c8\ub2e4&#8230;&quot;) while True: # sockets_list\uc5d0 \uc788\ub294 \uc18c\ucf13\ub4e4 \uc911 \uc77d\uae30(\uc774\ubca4\ud2b8 \ubc1c\uc0dd)\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=11766\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-11766","post","type-post","status-publish","format-standard","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/11766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11766"}],"version-history":[{"count":1,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/11766\/revisions"}],"predecessor-version":[{"id":11767,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/11766\/revisions\/11767"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}