import socket # 客戶端 傳送乙個資料,再接收乙個資料
import time
import threading
import ctypes
import inspect
quit =
0# 終止執行緒
def_async_raise
(tid, exctype)
:"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
ifnot inspect.isclass(exctype)
: exctype =
type
(exctype)
res = ctypes.pythonapi.pythreadstate_setasyncexc(
tid, ctypes.py_object(exctype)
)if res ==0:
raise valueerror(
"invalid thread id"
)elif res !=1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=null to revert the effect"""
ctypes.pythonapi.pythreadstate_setasyncexc(tid,
none
)raise systemerror(
"pythreadstate_setasyncexc failed"
)def
stop_thread
(thread)
: _async_raise(thread.ident, systemexit)
defreceivemsg
(conn)
:global quit
while
true
:try
: data = conn.recv(
1024
)except
:print
("連線結束"
) conn.close(
)break
ifstr
(data.decode())
.upper()!=
'quit'
:print
('recive:'
, data.decode())
else
: quit =
1 conn.close(
)break
defsendmsg
(conn)
:global quit
while
true
: send =
input
("send:\n"
)try
: conn.send(send.encode(
'utf-8'))
except
:print
("連線結束"
) conn.close(
)break
ifstr
(send)
.upper()==
'quit'
: conn.close(
)break
# 改寫執行緒
class
msgthread
(threading.thread)
:def
__init__
(self, conn, flag)
: threading.thread.__init__(self)
self.conn = conn
self.flag = flag
defrun(self)
:if self.flag ==1:
receivemsg(self.conn)
else
: sendmsg(self.conn)
# 宣告socket型別,同時生成鏈結物件
defclient
(address)
: client = socket.socket(socket.af_inet, socket.sock_stream)
while
true
:try
: client.connect(
(address,
6999))
# 建立乙個鏈結,連線到本地的6999埠
break
except
:print
("等待偵聽!"
) time.sleep(1)
thread_receive = msgthread(client,1)
thread_send = msgthread(client,2)
thread_receive.start(
) thread_send.start(
)def
server()
: server = socket.socket(socket.af_inet, socket.sock_stream)
server.bind(
('127.0.0.1'
,6999))
# 繫結要監聽的埠
server.listen(5)
# 開始監聽 表示可以使用五個鏈結排隊
conn, addr = server.accept(
)# 等待鏈結,多個鏈結的時候就會出現問題,其實返回了兩個值
print
("偵聽器已啟動!port:6999"
)print
(conn, addr)
thread_receive = msgthread(conn,1)
thread_send = msgthread(conn,2)
thread_receive.start(
) thread_send.start(
)if __name__ ==
"__main__"
: bootmode =
input
("請選擇啟動方式(1或2):\n"
)if bootmode ==
'1':
server(
)else
: port =
input()
client(port)
ElasticSearch 高效能分布式搜尋引擎
elasticsearch 簡稱 es 是一款高效能的實時分布式搜尋和分析引擎,它可以從海量資料中快速的找到相關資訊。作為一款功能強大的分布式搜尋引擎,支援近實時的儲存 搜尋資料。被許多公司廣泛的應用 elasticsearch 有幾個核心概念,這裡先講解幾個概念,對下面的學習過程比較有幫助 型別表...
ClickHouse高效能分布式分析資料庫
向大家介紹clickhouse的基本特性和概念,特別是效能對比資料,包括和hive spark druid的效能對比。文章資源來自示說網 熱門資源 rabbitmq大型電商 實踐 rabbitmq實戰指南 作者朱小廝為你介紹rabbitmq在大型電商 中的實踐經驗,具體介紹了生產者 消費者 brok...
ClickHouse高效能分布式分析資料庫
向大家介紹clickhouse的基本特性和概念,特別是效能對比資料,包括和hive spark druid的效能對比。文章資源來自示說網 熱門資源 rabbitmq大型電商 實踐 www.slidestalk.com s rabbitmq2 rabbitmq實戰指南 作者朱小廝為你介紹rabbitm...