python 网络编程-01基础

python 网络编程

// 服务端 server.py
import socket

if __name__ == '__main__':
    server = socket.socket()
    server.bind(('127.0.0.1', 8000))

    server.listen(5)

    while True:
        conn, addr = server.accept()
        msg = conn.recv(1024)
        print(msg)
        answer = input('请输入:')
        conn.send(answer.encode('utf-8'))

//客户端 client.py

import socket

if __name__ == '__main__':
    client = socket.socket()
    client.connect(('127.0.0.1', 8000))
    client.send(b'hello')
    msg = client.recv(1024)
    print(msg.decode('utf-8'))
    client.close()
posted @ 2020-03-02 21:13  hurz  阅读(81)  评论(0编辑  收藏  举报