风-fmgao

导航

socket通信循环

server-----------------
#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/6/5


import socket

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.bind(('127.0.0.1', 8081))
phone.listen(5)

print('starting.......')
conn, client_addr = phone.accept()
print(client_addr)

while True:
    data = conn.recv(1024)
    print('客户端的数据', data)

    conn.send(data.upper())

conn.close()
phone.close()

client--------------------
#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/6/5


import socket

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.connect(('127.0.0.1', 8081))

while True:
    msg = input('>>>').strip()
    phone.send(msg.encode('utf-8'))
    data = phone.recv(1024)
    print(data.decode('utf-8'))

phone.close()



posted on 2018-06-15 23:50  风-fmgao  阅读(128)  评论(0编辑  收藏  举报