• Python之socket编程


    本章内容

      1、socket

      2、IO多路复用

      3、socketserver

    Socket

    socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用【打开】【读写】【关闭】模式来操作。socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)

    基本上,Socket 是任何一种计算机网络通讯中最基础的内容。例如当你在浏览器地址栏中输入 http://www.cnblogs.com/ 时,你会打开一个套接字,然后连接到 http://www.cnblogs.com/ 并读取响应的页面然后然后显示出来。而其他一些聊天客户端如 gtalk 和 skype 也是类似。任何网络通讯都是通过 Socket 来完成的。

    socket和file的区别:

      1、file模块是针对某个指定文件进行【打开】【读写】【关闭】

      2、socket模块是针对 服务器端 和 客户端Socket 进行【打开】【读写】【关闭】

    那我们就先来创建一个socket服务端吧

     server

    1. import socket
    2. sk = socket.socket()
    3. sk.bind(("127.0.0.1",8080))
    4. sk.listen(5)
    5. conn,address = sk.accept()
    6. sk.sendall(bytes("Hello world",encoding="utf-8"))
    7. server

     View Code

    1. import socket
    2. obj = socket.socket()
    3. obj.connect(("127.0.0.1",8080))
    4. ret = str(obj.recv(1024),encoding="utf-8")
    5. print(ret)

    socket更多功能

     更多功能

    1. def bind(self, address): # real signature unknown; restored from __doc__
    2. """
    3. bind(address)
    4. Bind the socket to a local address. For IP sockets, the address is a
    5. pair (host, port); the host must refer to the local host. For raw packet
    6. sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
    7. """
    8. '''将套接字绑定到本地地址。是一个IP套接字的地址对(主机、端口),主机必须参考本地主机。'''
    9. pass
    10. def close(self): # real signature unknown; restored from __doc__
    11. """
    12. close()
    13. Close the socket. It cannot be used after this call.
    14. """
    15. '''关闭socket'''
    16. pass
    17. def connect(self, address): # real signature unknown; restored from __doc__
    18. """
    19. connect(address)
    20. Connect the socket to a remote address. For IP sockets, the address
    21. is a pair (host, port).
    22. """
    23. '''将套接字连接到远程地址。IP套接字的地址'''
    24. pass
    25. def connect_ex(self, address): # real signature unknown; restored from __doc__
    26. """
    27. connect_ex(address) -> errno
    28. This is like connect(address), but returns an error code (the errno value)
    29. instead of raising an exception when an error occurs.
    30. """
    31. pass
    32. def detach(self): # real signature unknown; restored from __doc__
    33. """
    34. detach()
    35. Close the socket object without closing the underlying file descriptor.
    36. The object cannot be used after this call, but the file descriptor
    37. can be reused for other purposes. The file descriptor is returned.
    38. """
    39. '''关闭套接字对象没有关闭底层的文件描述符。'''
    40. pass
    41. def fileno(self): # real signature unknown; restored from __doc__
    42. """
    43. fileno() -> integer
    44. Return the integer file descriptor of the socket.
    45. """
    46. '''返回整数的套接字的文件描述符。'''
    47. return 0
    48. def getpeername(self): # real signature unknown; restored from __doc__
    49. """
    50. getpeername() -> address info
    51. Return the address of the remote endpoint. For IP sockets, the address
    52. info is a pair (hostaddr, port).
    53. """
    54. '''返回远程端点的地址。IP套接字的地址'''
    55. pass
    56. def getsockname(self): # real signature unknown; restored from __doc__
    57. """
    58. getsockname() -> address info
    59. Return the address of the local endpoint. For IP sockets, the address
    60. info is a pair (hostaddr, port).
    61. """
    62. '''返回远程端点的地址。IP套接字的地址'''
    63. pass
    64. def getsockopt(self, level, option, buffersize=None): # real signature unknown; restored from __doc__
    65. """
    66. getsockopt(level, option[, buffersize]) -> value
    67. Get a socket option. See the Unix manual for level and option.
    68. If a nonzero buffersize argument is given, the return value is a
    69. string of that length; otherwise it is an integer.
    70. """
    71. '''得到一个套接字选项'''
    72. pass
    73. def gettimeout(self): # real signature unknown; restored from __doc__
    74. """
    75. gettimeout() -> timeout
    76. Returns the timeout in seconds (float) associated with socket
    77. operations. A timeout of None indicates that timeouts on socket
    78. operations are disabled.
    79. """
    80. '''返回的超时秒数(浮动)与套接字相关联'''
    81. return timeout
    82. def ioctl(self, cmd, option): # real signature unknown; restored from __doc__
    83. """
    84. ioctl(cmd, option) -> long
    85. Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are
    86. SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.
    87. SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).
    88. """
    89. return 0
    90. def listen(self, backlog=None): # real signature unknown; restored from __doc__
    91. """
    92. listen([backlog])
    93. Enable a server to accept connections. If backlog is specified, it must be
    94. at least 0 (if it is lower, it is set to 0); it specifies the number of
    95. unaccepted connections that the system will allow before refusing new
    96. connections. If not specified, a default reasonable value is chosen.
    97. """
    98. '''使服务器能够接受连接。'''
    99. pass
    100. def recv(self, buffersize, flags=None): # real signature unknown; restored from __doc__
    101. """
    102. recv(buffersize[, flags]) -> data
    103. Receive up to buffersize bytes from the socket. For the optional flags
    104. argument, see the Unix manual. When no data is available, block until
    105. at least one byte is available or until the remote end is closed. When
    106. the remote end is closed and all data is read, return the empty string.
    107. """
    108. '''当没有数据可用,阻塞,直到至少一个字节是可用的或远程结束之前关闭。'''
    109. pass
    110. def recvfrom(self, buffersize, flags=None): # real signature unknown; restored from __doc__
    111. """
    112. recvfrom(buffersize[, flags]) -> (data, address info)
    113. Like recv(buffersize, flags) but also return the sender's address info.
    114. """
    115. pass
    116. def recvfrom_into(self, buffer, nbytes=None, flags=None): # real signature unknown; restored from __doc__
    117. """
    118. recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)
    119. Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.
    120. """
    121. pass
    122. def recv_into(self, buffer, nbytes=None, flags=None): # real signature unknown; restored from __doc__
    123. """
    124. recv_into(buffer, [nbytes[, flags]]) -> nbytes_read
    125. A version of recv() that stores its data into a buffer rather than creating
    126. a new string. Receive up to buffersize bytes from the socket. If buffersize
    127. is not specified (or 0), receive up to the size available in the given buffer.
    128. See recv() for documentation about the flags.
    129. """
    130. pass
    131. def send(self, data, flags=None): # real signature unknown; restored from __doc__
    132. """
    133. send(data[, flags]) -> count
    134. Send a data string to the socket. For the optional flags
    135. argument, see the Unix manual. Return the number of bytes
  • 相关阅读:
    【前段工程化】经验总结03-ESNext到底是个啥?tsconfig.json配置文件中的ESNext, ESNext和ES6的关系
    Springboot企业人力资源管理系统j863o计算机毕业设计-课程设计-期末作业-毕设程序代做
    C语言08、数据在内存中的存储、大小端存储模式
    HttpServletQequest与HttpServletQespons的常用方法!
    mysql远程连接失败
    1 函数编程题
    【ARM Trace32(劳特巴赫) 使用介绍 5 -- Trace32 scan dump 详细介绍】
    自定义注解
    Java进阶学习
    移动端异构运算技术 - GPU OpenCL 编程(基础篇)
  • 原文地址:https://blog.csdn.net/m0_59485658/article/details/128189415