day_5.28 py网络编程

端口

 

socket简介:

socket为一个类   s接收的是返回的对象引用

 

2018-5-28 15:52:47
开始进行网络编程
udp 套接字
encode() 编码 decode() 解码
'''
from socket import *

#创建一个udp套接字
udpSocket = socket(AF_INET,SOCK_DGRAM)
# 使用udp发送的数据,在每一次的都需要写上接收方的ip和port
udpSocket.sendto(b"haha",("192.168.19.15",8080)) #传入参数内容,("IP",端口号)
# 绑定端口,如果不绑定,则系统分配 (接收方需要绑定数据,发送方不需要绑定)
udpSocket.bind("",7788)
#等待接受对方发送的数据
recvDate = udpSocket.recvfrom(1021) #1024表示本次接收的最大字节数
#接收数据为元组:  (数据,ip)
content,destInfo = recvDate
print("content is %s"%content.decode("utf-8"))
# 显式接收的数据
print(recvDate)
#创建一个tcp套接字
# tcpSocket = socket.socket(AF_INET,SOCK_STREAM)

#udp套接字发送数据优化 解决第14行在数据前加b的问题(python3会出现)
udpSocket = socket(AF_INET,SOCK_DGRAM)
destIP = input("请输入目的ip:")
destPort = int(input("请输入目的port:"))
sendData = input("请输入要发送的数据:")
udpSocket.sendto(sendData.encode("utf-8"),(destInfo,destPort))

  

2018-5-28 15:52:47
开始进行网络编程
udp 套接字
encode() 编码 decode() 解码
'''
from socket import *

#创建一个udp套接字
udpSocket = socket(AF_INET,SOCK_DGRAM)
# 使用udp发送的数据,在每一次的都需要写上接收方的ip和port
udpSocket.sendto(b"haha",("192.168.19.15",8080)) #传入参数内容,("IP",端口号)
# 绑定端口,如果不绑定,则系统分配 (接收方需要绑定数据,发送方不需要绑定)
udpSocket.bind("",7788)
#等待接受对方发送的数据
recvDate = udpSocket.recvfrom(1021) #1024表示本次接收的最大字节数
#接收数据为元组: (数据,ip)
content,destInfo = recvDate
print("content is %s"%content.decode("utf-8"))
# 显式接收的数据
print(recvDate)
#创建一个tcp套接字
# tcpSocket = socket.socket(AF_INET,SOCK_STREAM)

#udp套接字发送数据优化 解决第14行在数据前加b的问题(python3会出现)
udpSocket = socket(AF_INET,SOCK_DGRAM)
destIP = input("请输入目的ip:")
destPort = int(input("请输入目的port:"))
sendData = input("请输入要发送的数据:")
udpSocket.sendto(sendData.encode("utf-8"),(destInfo,destPort))
posted @ 2018-05-28 15:40  我想喝杨枝甘露~  阅读(203)  评论(0编辑  收藏  举报