python进阶(2)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42194337/article/details/98973532

python进阶(2)

udp接收数据

    def sendto(self, data, flags=None, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
        """
        sendto(data[, flags], address) -> count
        
        Like send(data, flags) but allows specifying the destination address.
        For IP sockets, the address is a pair (hostaddr, port).
        """
        pass

注意点:

  1. sendto(),这个方法发送的数据,是bytes类型,普通字符串用encode()转化成bytes
  2. address,用来指定ip和端口,打包成元组
    def recvfrom(self, buffersize, flags=None): # real signature unknown; restored from __doc__
        """
        recvfrom(buffersize[, flags]) -> (data, address info)
        
        Like recv(buffersize, flags) but also return the sender's address info.
        """
        pass

注意点:

  1. 返回值,返回(数据,发送该信息的地址)
  2. 数据接收是bytes类型的,需要用decode(),转换成普通的字符串

bind()

    def bind(self, address): # real signature unknown; restored from __doc__
        """
        bind(address)
        
        Bind the socket to a local address.  For IP sockets, the address is a
        pair (host, port); the host must refer to the local host. For raw packet
        sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
        """
        pass

注意点:

  1. '',代表本机任意地址都可以

TCP协议

基于TCP协议的客户端

  1. 创建套接字
  2. 与服务端创建连接
  3. 发送数据
  4. 接收数据
  5. 关闭套接字

基于TCP协议的服务端

  1. 创建socket套接字
  2. bind,绑定服务端的ip和端口
  3. listen,监听,使服务端的套接字从主动状态变成被动状态,等待连接
  4. accept,等待客户端的连接,运行之后是阻塞状态,直到有连接
  5. 收发数据
  6. 关闭套接字
展开阅读全文

没有更多推荐了,返回首页