Python-鍩轰簬socket鍜宻elect妯″潡瀹炵幇IO澶氳矾澶嶇敤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-鍩轰簬socket鍜宻elect妯″潡瀹炵幇IO澶氳矾澶嶇敤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%90%af%e5%8a%a8%e6%9c%8d%e5%8a%a1' title='鍚姩鏈嶅姟'>鍚姩鏈嶅姟
soc obj 鏀寔 enc 涔嬩竴 close 杩斿洖 鐢ㄦ埛鈥樷€樷€業O鎸囩殑鏄緭鍏ヨ緭鍑猴紝涓€閮ㄥ垎鎸囩殑鏄枃浠舵搷浣滐紝杩樻湁涓€閮ㄥ垎
缃戠粶浼犺緭鎿嶄綔锛屼緥濡俿oekct灏辨槸鍏朵腑涔嬩竴锛涘璺鐢ㄦ寚鐨勬槸
鍒╃敤涓€绉嶆満鍒讹紝鍚屾椂浣跨敤澶氫釜IO锛屼緥濡傚悓鏃剁洃鍚涓枃浠跺彞
鏌勶紙socket瀵硅薄涓€鏃︿紶閫佹垨鑰呮帴鏀朵俊鎭級锛屼竴鏃︽枃浠跺彞鏌勫嚭
鐜板彉鍖栧氨浼氱珛鍒绘劅鐭ュ埌
鈥樷€樷€?br>1.涓嬮潰閫氳繃IO澶氳矾澶嶇敤瀹炵幇澶氫汉鍚屾椂杩炴帴socket鏈嶅姟鍣?/pre>import socket sk1 = socket.socket()#sk1,sk2,sk3杩欏氨鏄竴涓枃浠舵弿杩扮 sk1.bind((鈥?/span>127.0.0.1鈥?/span>,8002)) sk1.listen() sk2 = socket.socket() sk2.bind((鈥?/span>127.0.0.1鈥?/span>,8003)) sk2.listen() sk3 = socket.socket() sk3.bind((鈥?/span>127.0.0.1鈥?/span>,8004)) sk3.listen() inputs = [sk1,sk2,sk3] import select while True: 鈥樷€樷€?/span>[sk1,sk2,sk3],select鍐呴儴浼氳嚜鍔ㄧ洃鍚瑂k1,sk21锛宻k3涓変釜瀵硅薄 涓€鏃︽煇涓彞鏌勫彂鐢熷彉鍖栧氨浼氳鐩戝惉鍒? 鈥樷€樷€?/span> #濡傛灉鏈変汉閾炬帴sk1锛屽垯浼氳娣诲姞杩涘垪琛紝r_list = [sk1] r_list,w_list,e_list = select.select(inputs,[],[],1)#1琛ㄧず绛変竴绉掞紝鍦╳hile鎵ц鍒拌繖閲岀殑鏃跺€欑洃娴嬩竴绉掞紝娌℃湁浜烘潵閾炬帴鐨勮瘽灏辨帴鐫€寰幆 #print(r_list,sk1.listen()) for sk in r_list: conn,address = sk.accept() #print(conn,address) conn.sendall(bytes(鈥?/span>浣犲ソ鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)) conn.close()#瀹㈡埛绔竴 import socket obj = socket.socket() obj.connect((鈥?/span>127.0.0.1鈥?/span>,8002)) content = str(obj.recv(1024),encoding=鈥?/span>utf-8鈥?/span>) print(content) obj.close() #瀹㈡埛绔簩 import socket obj = socket.socket() obj.connect((鈥?/span>127.0.0.1鈥?/span>,8003)) content = str(obj.recv(1024),encoding=鈥?/span>utf-8鈥?/span>) print(content) obj.close() #瀹㈡埛绔笁 import socket obj = socket.socket() obj.connect((鈥?/span>127.0.0.1鈥?/span>,8004)) content = str(obj.recv(1024),encoding=鈥?/span>utf-8鈥?/span>) print(content) obj.close()鎵ц缁撴灉锛氬彧瑕佸惎鍔ㄦ湇鍔″櫒绔紝鐒跺悗涓嶅悓鐨勫鎴风澶氭鍚姩閮借兘鏀跺埌淇℃伅锛屽涓鍙f垚鍔熻鐩戝惉
2. 涓嬮潰浣跨敤select妯″潡瀹炵幇澶氳矾澶嶇敤锛屼娇鍚屼竴涓鍙e悓鏃舵帴鏀跺涓摼鎺?/p>
import socket sk1 = socket.socket()#sk1,sk2,sk3杩欏氨鏄竴涓枃浠舵弿杩扮 sk1.bind((鈥?/span>127.0.0.1鈥?/span>,8002)) sk1.listen() # # sk2 = socket.socket() # sk2.bind((鈥?27.0.0.1鈥?8003)) # sk2.listen() # # sk3 = socket.socket() # sk3.bind((鈥?27.0.0.1鈥?8004)) # sk3.listen() inputs = [sk1] import select #epoll鏁堢巼鏇撮珮锛屼絾鏄疻indows涓嶆敮鎸侊紝瀹冩槸璋佹湁闂灏卞憡璇夊畠锛屼笉鐢ㄥ惊鍧?/span> while True: 鈥樷€樷€?/span>[sk1,sk2,sk3],select鍐呴儴浼氳嚜鍔ㄧ洃鍚瑂k1,sk21锛宻k3涓変釜瀵硅薄 涓€鏃︽煇涓彞鏌勫彂鐢熷彉鍖栵紙鏌愪汉鏉ラ摼鎺ワ級灏变細琚洃鍚埌 鈥樷€樷€?/span> #濡傛灉鏈変汉閾炬帴sk1锛屽垯浼氳娣诲姞杩涘垪琛紝r_list = [sk1] r_list,w_list,e_list = select.select(inputs,[],[],1) 鈥樷€樷€?/span>绗笁涓弬鏁版槸鐩戝惉閿欒鐨勶紝鍙鏈夐敊璇嚭鐜帮紝灏变細琚洃鍚埌锛岃繑鍥瀍_list 绗簩涓弬鏁拌繑鍥炵粰w_list锛屽彧瑕佷紶浜嗕粈涔堬紝灏卞師灏佷笉鍔ㄧ殑浼犵粰w_list鈥樷€樷€?/span> print(鈥?/span>姝e湪鐩戝惉 %s 澶氬皯涓璞?/span>鈥?/span> % len(inputs)) for sk in r_list: if sk == sk1: #鍙ユ焺璺熸湇鍔″櫒绔殑瀵硅薄涓€鏍凤紝琛ㄧず鏈夋柊鐢ㄦ埛鏉ラ摼鎺ヤ簡 conn,address = sk.accept() inputs.append(conn)#鍔犲叆鍘讳箣鍚庯紝inputs鏈変竴涓摼鎺ュ璞″拰鏈嶅姟鍣ㄥ璞?/span> else: #鏈夎€佺敤鎴峰彂娑堟伅浜?/span> try: data_byte =sk.recv(1024) data_str =str(data_byte,encoding=鈥?/span>utf-8鈥?/span>) sk.sendall(bytes(data_str+鈥?/span>鏀跺埌浜?/span>鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)) except:#绌轰俊鎭〃绀哄鎴风鏂紑閾炬帴锛屾墍浠ヨ鍦ㄧ洃鍚腑绉婚櫎 inputs.remove(sk)#杩欓噷鐨剆k灏辨槸涔嬪墠浼犺繘鍘荤殑conn锛屽洜涓簉_list鎺ユ敹鐨勬槸鏈夊彉鍖栫殑鍊?/span>鍚姩杩欎釜鏈嶅姟绔箣鍚庯紝灏卞彲浠ュ疄鐜板璺鐢ㄤ簡锛屽彲浠ユ帴鏀跺涓鎴风鍚屾椂杩炴帴
3.涓嬮潰浠嬬粛涓€浜涘璺搷浣滈噷闈㈢殑璇诲啓鍒嗙
import socket sk1 = socket.socket()#sk1灏辨槸涓€涓枃浠舵弿杩扮 sk1.bind((鈥?/span>127.0.0.1鈥?/span>,8002)) sk1.listen() inputs = [sk1]#琚娴嬪彂鐢熷彉鍔ㄧ殑鍙ユ焺鏀捐繖閲?/span> outputs=[]#鐢ㄦ潵璁板綍璋佺粰鏈嶅姟绔彂杩囦俊鎭紝浠ヤ究鍥炲 message_dict = {}#鎺ユ敹淇℃伅鐨勶紝鏍规嵁鍙ユ焺褰㈡垚閿€煎 import select #epoll鏁堢巼鏇撮珮锛屼絾鏄疻indows涓嶆敮鎸侊紝瀹冩槸璋佹湁闂灏卞憡璇夊畠锛屼笉鐢ㄥ惊鍧?/span> while True: 鈥樷€樷€?/span>[sk1,sk2,sk3],select鍐呴儴浼氳嚜鍔ㄧ洃鍚瑂k1,sk21锛宻k3涓変釜瀵硅薄 涓€鏃︽煇涓彞鏌勫彂鐢熷彉鍖栵紙鏌愪汉鏉ラ摼鎺ワ級灏变細琚洃鍚埌 鈥樷€樷€?/span> #濡傛灉鏈変汉閾炬帴sk1锛屽垯浼氳娣诲姞杩涘垪琛紝r_list = [sk1] r_list,w_list,e_list = select.select(inputs,outputs,inputs,1) 鈥樷€樷€?/span>绗笁涓弬鏁版槸鐩戝惉閿欒鐨勶紝鍙鏈夐敊璇嚭鐜帮紝灏变細琚洃鍚埌锛岃繑鍥瀍_list 绗簩涓弬鏁拌繑鍥炵粰w_list锛屽彧瑕佷紶浜嗕粈涔堬紝灏卞師灏佷笉鍔ㄧ殑浼犵粰w_list鈥樷€樷€?/span> print(鈥?/span>姝e湪鐩戝惉 %s 澶氬皯涓璞?/span>鈥?/span> % len(inputs)) for sk in r_list: if sk == sk1: #鍙ユ焺璺熸湇鍔″櫒绔殑瀵硅薄涓€鏍凤紝琛ㄧず鏈夋柊鐢ㄦ埛鏉ラ摼鎺ヤ簡 conn,address = sk.accept() inputs.append(conn)#鍔犲叆鍘讳箣鍚庯紝inputs鏈変竴涓摼鎺ュ璞″拰鏈嶅姟鍣ㄥ璞?/span> message_dict[conn]=[]#瀛楀吀鐨刱ey鏄叿浣撶殑閾炬帴瀵硅薄 else: #鏈夎€佺敤鎴峰彂娑堟伅浜?/span> try: data_byte =sk.recv(1024) except Exception as e:#绌轰俊鎭〃绀哄鎴风鏂紑閾炬帴锛屾墍浠ヨ鍦ㄧ洃鍚腑绉婚櫎 print(e) inputs.remove(sk)#杩欓噷鐨剆k灏辨槸涔嬪墠浼犺繘鍘荤殑conn锛屽洜涓簉_list鎺ユ敹鐨勬槸鏈夊彉鍖栫殑鍊?/span> else: data_str = str(data_byte, encoding=鈥?/span>utf-8鈥?/span>) message_dict[sk].append(data_str) outputs.append(sk)#鎶婁紶閫佹暟鎹殑鍙ユ焺娣诲姞杩涘幓绗簩涓弬鏁?/span> for conn in w_list: recv_str=message_dict[conn][0]#鎷垮埌鎴戜滑鏀跺埌浜嗙殑淇℃伅 del message_dict[conn][0]#鍒犻櫎淇℃伅锛岄槻姝笅娆″嚭鐜颁竴鏍风殑娑堟伅 print(recv_str) conn.sendall(bytes(recv_str+鈥?/span>鏀跺埌浜?/span>鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)) outputs.remove(conn)#鍥炲瀹屾垚涔嬪悗鍦ㄥ垪琛ㄥ垹闄?/span> for sk_or_conn in e_list: e_list.remove(sk_or_conn)杩欐牱鍙互褰㈡垚绠€鍗曠殑璇诲啓鍒嗙鎿嶄綔
以上是关于Python-鍩轰簬socket鍜宻elect妯″潡瀹炵幇IO澶氳矾澶嶇敤的主要内容,如果未能解决你的问题,请参考以下文章
PHP鍩轰簬MVC妯″紡涓嬬殑鍖栧鍝侀攢鍞綉绔欙紝婧愮爜鍒嗕韩
PHP鍩轰簬MVC妯″紡涓嬬殑鍋滆溅鍦鸿溅浣嶇鐞嗙郴缁熴€?48婧愮爜涓嬭浇
Spark + Hadoop,鍩轰簬WIFI鎺㈤拡鐨勫ぇ鏁版嵁鍒嗘瀽绯荤粺