socket套接字小荔枝

通信循环 客户端

import socket

phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

phone.connect(("127.0.0.1",8066))

import time

time.sleep(3)

while True:

    msg=input("input your msg >>:").strip()

     if msg=="q":break

    if len(msg)==0:continue

    phone.send(msg.encode("utf-8"))

    data=phone.recv(1024)

    print(data.decode("utf-8"))

 

phone.close()

通信循环服务端

import socket

phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

phone.bind(("127.0.0.1",8066))

phone.listen(5)

print("服务端正在监听:%s:%s"%("127.0.0.1",8066))

conn,client_addr=phone.accept()

print("客户端IP和端口为:",client_addr)

 

while True:

    try:

        data=conn.recv(1024)

        if len(data)==0:break

        print("客户端发送的消息为:",data.decode("utf-8"))

        conn.send(data.upper())

    except Exception:

        break

conn.close()

phone.close()

posted @ 2020-04-20 22:24  五月雨变奏  阅读(122)  评论(0编辑  收藏  举报