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
- 求两个直方图的卷积 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
