码迷,mamicode.com
首页 > 编程语言 > 详细

python 网络编程-02 多进程的交互案例

时间:2020-03-03 09:19:17      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:get   end   answer   cep   tar   cli   bye   recv   进程   

server.py

import socket
from threading import Thread


def Server():
    server = socket.socket()
    server.bind(('127.0.0.1', 8000))
    server.listen(5)

    while True:
        conn, addr = server.accept()
        Thread(target=talk,args=[conn,]).start()


def talk(connect):
    while True:
        msg = connect.recv(1024)
        print(msg)
        if msg.decode('utf-8') == 'bye':
            break
        answer = "我是server!"
        connect.send(answer.encode('utf-8'))
    connect.close()


if __name__ == '__main__':
    Server()

client.py

import socket

if __name__ == '__main__':
    client = socket.socket()
    client.connect(('127.0.0.1', 8000))
    while True:
        send_msg = input("请输入")
        client.send(send_msg.encode())
        if send_msg == "bye":
            break
        msg = client.recv(1024)
        print(msg.decode('utf-8'))

    client.close()

python 网络编程-02 多进程的交互案例

标签:get   end   answer   cep   tar   cli   bye   recv   进程   

原文地址:https://www.cnblogs.com/pythonPath/p/12399225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!