socket学习

Posted on 2018-02-26 10:14  阿骏骏  阅读(82)  评论(0编辑  收藏  举报

对应的代码:

服务端:

import  socket

phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #买手机
phone.bind(('127.0.0.1',8000))#绑定手机卡

phone.listen(5)#开机
print('——>')
conn,addr = phone.accept()# 等电话

msg = conn.recv(1024) #收消息
print('客户端发来的消息是:',msg)
conn.send(msg.upper())#发消息

conn.close() #挂电话
phone.close() #关机
服务端

客户端:

import socket


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

phone.connect(('127.0.0.1',8000))

phone.send('hello,this is the first netprogram'.encode('utf-8'))
data = phone.recv(1024)

print('收到从服务端发来的消息:',data)
客户端

 

Copyright © 2024 阿骏骏
Powered by .NET 9.0 on Kubernetes