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鎺㈤拡鐨勫ぇ鏁版嵁鍒嗘瀽绯荤粺

NLP-BERT 璋锋瓕鑷劧璇█澶勭悊妯″瀷锛欱ERT-鍩轰簬pytorch

linux鐜鍩轰簬python璇█docx杞琾df

鍩轰簬璇嶅吀鍜屾湸绱犺礉鍙舵柉涓枃鎯呮劅鍊惧悜鍒嗘瀽绠楁硶