python 网络编程-02 多进程的交互案例
Posted hurz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 网络编程-02 多进程的交互案例相关的知识,希望对你有一定的参考价值。
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 多进程的交互案例的主要内容,如果未能解决你的问题,请参考以下文章