[python]代码库
import threading
import time
import logging
import socket
logging.basicConfig(format='%(thread)s %(threadName)s %(message)s',level=logging.INFO)
#创建一个tcpserver对象
sock = socket.socket() #1.创建一个tcpserver对象
ip = '127.0.0.1'
port = 9999
addr = (ip,port)
sock.bind(addr) #2.绑定
sock.listen() #3.监听
conn,addrinfo = sock.accept() #4.接收消息,等待访问,阻塞状态
logging.info(conn)
logging.info(addrinfo)
data = conn.recv(1024) #阻塞中,
logging.info(data.decode())
msg = 'ack {}'.format(data.decode())
conn.send(msg.encode()) #发送给客户端消息,data是字节
logging.info(sock)
conn.close()
sock.close()