python中Socket.accept()的返回值是什么

What#39;s the return value of Socket.accept() in python(python中Socket.accept()的返回值是什么)

本文介绍了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()的返回值是什么

基础教程推荐