What#39;s the return value of Socket.accept() in python(python中Socket.accept()的返回值是什么)
问题描述
我用python中的socket
模块做了一个简单的服务器和一个简单的客户端.
I made a simple server and a simple client with socket
module in python.
服务器:
# server.py
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for your connecting')
c.close()
和客户:
#client.py
import socket
s = socket.socket()
host = socket.socket()
port = 1234
s.connect((host, port))
print s.recv(1024)
我启动了服务器,然后启动了 4 个客户端,并在服务器的控制台中得到如下输出:
I started the server and then started 4 clients and got output in server's console as below:
Got connection from ('192.168.0.99', 49170)
Got connection from ('192.168.0.99', 49171)
Got connection from ('192.168.0.99', 49172)
Got connection from ('192.168.0.99', 49173)
元组的第二部分是什么?
what is the second part in the tuple?
推荐答案
来自 socket
文档:
From the socket
documentation:
AF_INET 地址族使用一对 (host, port),其中 host 是一个字符串,表示 Internet 域表示法中的主机名,如daring.cwi.nl"或 IPv4 地址,如100.50.200.5",端口是一个整数.
A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.
所以第二个值是客户端用于连接的端口号.当建立 TCP/IP 连接时,客户端选择一个传出端口号与服务器通信;服务器返回的数据包将被寻址到该端口号.
So the second value is the port number used by the client side for the connection. When a TCP/IP connection is established, the client picks an outgoing port number to communicate with the server; the server return packets are to be addressed to that port number.
这篇关于python中Socket.accept()的返回值是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python中Socket.accept()的返回值是什么


基础教程推荐
- 对多索引数据帧的列进行排序 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01