zoukankan      html  css  js  c++  java
  • 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))
  • 相关阅读:
    Ubuntu 14.04设置开机启动脚本的方法
    python 筛选
    分段压缩
    ubuntu 16.04 启用root用户方法
    Ubuntu 16.04 设置MySQL远程访问权限
    [分享]在ubuntu9.10下实现开机自动登录并运行自己的图形程序
    ubuntu live cd修复grub引导项
    安装dcm4chee-arc-light-5.4.1-mysql步骤
    数据库学习--wildfly配置postgreSQL数据源
    wildfly配置PostgreSQL数据源
  • 原文地址:https://www.cnblogs.com/zhen1996/p/9100477.html
Copyright © 2011-2022 走看看