服務端:
import socket
import time
socket = socket.socket()
ip = socket.bind(('192.168.15.85',8000))
socket.listen(5)
conn,add = socket.accept()#阻塞等待
# while True:
# s = conn.recv(1024) # 接受bytes類型
# t = s.decode('utf-8') #轉換成字符串
# dd = time.localtime(float(t))
# td = time.strftime("%Y/%m/%d %H:%M:%S",dd)
# print(td)
# # input('請輸入聊天文字:')
# s='發送'
# # conn.send(s.encode('utf-8'))
# socket.close()
#
# while True:
# s = conn.recv(1024)
# t = s.decode('utf-8')
# print('收到:%s' % t)
# ss = input('輸入框:')
# ts =conn.send(ss.encode('utf-8'))
客戶端:
import socketimport timesocket = socket.socket()ip = ('192.168.15.85',8000)socket.connect(ip)# while True:# time.sleep(2)# s= time.time()# socket.send(str(s).encode('utf-8'))# ## # t = socket.recv(1024)#阻塞住 等待接受# # ts = t.decode('utf-8')# # print(ts)# socket.close()# while True:# s = input('輸入框:')# socket.send(s.encode('utf-8'))# o = socket.recv(1024)# oo = o.decode('utf-8')# print('收到:%s'% oo)