服务器
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#绑定IP端口
server.bind((IP地址, 端口号))
#监听
server.listen(5)
print("服务器启动成功******")
#等待链接
clientSocket, clientAddress = server.accept()
while True:
data = clientSocket.recv(1024)
print("收到"+str(clientSocket)+"数据"+data.decode("utf-8"))
clientSocket.send("".encode('utf-8'))
客户端
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((IP地址, 端口号))
count = 0
while True:
data=input("")
client.send(data.encode("utf-8"))
info = client.recv(1024)
print(info.decode("utf-8"))
#迭代器不但可以作用于for循环,还可以不断被next函数不断调用并返回下一个值
isinstance(变量,Iterator) #判断变量是否为iterator对象
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.connect((IP地址, 端口号))
udp.send(" ".encode("gbk"))