网络编程

*****************************************************************************2018.01.13************************************************************

————————————————————————网络编程套接字使用——————————————————————

发送数据
from socket import *
#1.创建套接字
udpSocket = socket(AF_INET,SOCK_STREAM)#TCP通讯方式 SOCK_DGRAM为UDP通讯方式
#2.准备接收方的地址
sendAddr = ('192.168.1.103', 8080)
#udpSocket.bind(sendAddr)绑定端口信息
#3.从键盘获取数据
sendData = input('请输入要发送的数据:')
#4.指定目标电脑
udpSocket.sendto(sendData,sendAddr)
#5.关闭套接字
udpSocket.close()
接受数据
from socket import *
#1.创建套接字
udpSocket = socket(AF_INET,SOCK_DGRAM)
#2.绑定本地相关信息
udpSocket.bind(('',7789))
#3.等待接受对方发送的数据
reveData =udpSocket.recvfrom(1024)
content= reveData[0] #或者是content, destInfo = receData
#4.显示接受到的数据
print('content is %s'%content.decode('utf-8'))
5.关闭套接字
udpSocket.close()
服务器
进程、线程、
协程:greenlet
gevent版服务器。遇到gevent.sleep(1)自动切换任务

posted on 2018-01-13 22:17  Jerry_Zhao  阅读(224)  评论(0编辑  收藏  举报

导航