python urlllib2包能指定程序的端口么
1个回答
展开全部
如果是tcp client用同一个本地端口去连不同的两个服务器ip,连第二个时就会提示端口已被占用。但服务器的监听端口,可以accept多次,建立多个socket;我的问题是服务器一个端口为什么能建立多个连接而客户端却不行呢?
TCP server 可以,TCP client 也可以。一个套接字只能建立一个连接,无论对于 server 还是 client。
注意报错消息是:
[Errno 106] (EISCONN) Transport endpoint is already connected
man 2 connect 说得很清楚了:
Generally, connection-based protocol sockets may successfully connect() only once; connectionless protocol sockets may use connect() multiple times to change their association.
就是说,TCP 套接字最多只能调用 connect 一次。那么,你的监听套接字调用 connect 了几次?
来点有意思的。
一个套接字不能连接两次,并不代表一个本地地址不能用两次,看!****加粗文字**加粗文字**
>>> import socket
>>> s = socket.socket()
# since Linux 3.9, 见 man 7 socket
>>> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
>>> s2 = socket.socket()
>>> s2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
>>> s.bind(('127.0.0.1', 12345))
>>> s2.bind(('127.0.0.1', 12345))
# 都可以使用同一本地地址来连接哦
>>> s.connect(('127.0.0.1', 80))
>>> s2.connect(('127.0.0.1', 4321))
>>> netstat -npt | grep 12345
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 127.0.0.1:4321 127.0.0.1:12345 ESTABLISHED 18284/python3
tcp 0 0 127.0.0.1:12345 127.0.0.1:4321 ESTABLISHED 4568/python3
tcp 0 0 127.0.0.1:80 127.0.0.1:12345 ESTABLISHED -
tcp 0 0 127.0.0.1:12345 127.0.0.1:80 ESTABLISHED 4568/python3
TCP server 可以,TCP client 也可以。一个套接字只能建立一个连接,无论对于 server 还是 client。
注意报错消息是:
[Errno 106] (EISCONN) Transport endpoint is already connected
man 2 connect 说得很清楚了:
Generally, connection-based protocol sockets may successfully connect() only once; connectionless protocol sockets may use connect() multiple times to change their association.
就是说,TCP 套接字最多只能调用 connect 一次。那么,你的监听套接字调用 connect 了几次?
来点有意思的。
一个套接字不能连接两次,并不代表一个本地地址不能用两次,看!****加粗文字**加粗文字**
>>> import socket
>>> s = socket.socket()
# since Linux 3.9, 见 man 7 socket
>>> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
>>> s2 = socket.socket()
>>> s2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
>>> s.bind(('127.0.0.1', 12345))
>>> s2.bind(('127.0.0.1', 12345))
# 都可以使用同一本地地址来连接哦
>>> s.connect(('127.0.0.1', 80))
>>> s2.connect(('127.0.0.1', 4321))
>>> netstat -npt | grep 12345
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 127.0.0.1:4321 127.0.0.1:12345 ESTABLISHED 18284/python3
tcp 0 0 127.0.0.1:12345 127.0.0.1:4321 ESTABLISHED 4568/python3
tcp 0 0 127.0.0.1:80 127.0.0.1:12345 ESTABLISHED -
tcp 0 0 127.0.0.1:12345 127.0.0.1:80 ESTABLISHED 4568/python3
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询