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()的返回值是什么
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
