• socket


    socket

    socket.socket([family[, type[, proto]]])

    The address family should be AF_INET (the default), AF_INET6 or AF_UNIX.

    The socket type should be SOCK_STREAM (the default), SOCK_DGRAM or perhaps one of the other SOCK_ constants.

    The protocol number is usually zero and may be omitted in that case.(通常忽略) 

    socket.getaddrinfo(host, port[, family[, socktype[, proto[, flags]]]])

      Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service.

      Host is a domain name, a string representation of an IPv4/v6 address or None.

      Port is a string service name such as 'http', a numeric port number or None.

      The family, socktype and proto arguments can be optionally specified in order to narrow the list of addresses returned. By default, their value is 0, meaning that the full range of results is selected.

      The flags argument can be one or several of the AI_* constants, and will influence how results are computed and returned. Its default value is 0. For example, AI_NUMERICHOST will disable domain name resolution and will raise an error if host is a domain name.

      The function returns a list of 5-tuples with the following structure:

      (family, socktype, proto, canonname, sockaddr)

      In these tuples, family, socktype, proto are all integers and are meant to be passed to the socket() function. canonname will be a string representing the canonical name of the host if   AI_CANONNAME is part of the flags argument; else canonname will be empty. sockaddr is a tuple describing a socket address, whose format depends on the returned family (a (address, port) 2-tuple for AF_INET, a (address, port, flow info, scope id) 4-tuple for AF_INET6), and is meant to be passed to the socket.connect() method.

    Example:

      

     【bind

      

      A couple things to notice: we used socket.gethostname() so that the socket would be visible to the outside world. If we had used s.bind(('localhost', 80)) or s.bind(('127.0.0.1', 80)) we would still have a “server” socket, but one that was only visible within the same machine. s.bind(('', 80)) specifies that the socket is reachable by any address the machine happens to have.

    flush

      There are two sets of verbs to use for communication. You can use send and recv, or you can transform your client socket into a file-like beast and use read and write. The latter is the way Java presents its sockets. I’m not going to talk about it here, except to warn you that you need to use flush on sockets. These are buffered “files”, and a common mistake is to write something, and then read for a reply. Without a flush in there, you may wait forever for the reply, because the request may still be in your output buffer.

     【send & recv

      Now we come to the major stumbling block of sockets - send and recv operate on the network buffers. They do not necessarily handle all the bytes you hand them (or expect from them), because their major focus is handling the network buffers. In general, they return when the associated network buffers have been filled (send) or emptied (recv). They then tell you how many bytes they handled. It is your responsibility to call them again until your message has been completely dealt with.

      When a recv returns 0 bytes, it means the other side has closed (or is in the process of closing) the connection. You will not receive any more data on this connection. Ever. You may be able to send data successfully.

      A protocol like HTTP uses a socket for only one transfer. The client sends a request, then reads a reply. That’s it. The socket is discarded. This means that a client can detect the end of the reply by receiving 0 bytes.

  • 相关阅读:
    “12306”的架构到底有多牛逼
    数字治理
    浅谈web网站架构演变过程
    MapReduce基本原理
    Flink+Hologres亿级用户实时UV精确去重最佳实践
    全链路压测体系建设方案的思考与实践
    如何做好一场技术演讲?
    “控本焦虑”的工程企业 用钉钉宜搭找到了低成本数字化的“捷径”
    友盟+《小程序用户增长白皮书》:从五个角度入手分析小程序数据
    数字化让618有了洞悉消费者内心的“大脑”
  • 原文地址:https://www.cnblogs.com/tekka/p/4551018.html
Copyright © 2020-2023  润新知