也不是不可以, 如果是socket的话只要设置端口复用就可以实现,随便写一段代码演示一下
#/bin/python
import sys
import time
import socket
def start_tcp_client(ip, port):
#server port and ip
server_ip = ip
servr_port = port
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 设置端口复用和地址复用
tcp_client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcp_client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
tcp_client.bind(('0.0.0.0', 23333))
tcp_client.connect((server_ip, servr_port))
# 没啥意义地一直接收数据
while True:
print tcp_client.recv(1024)
time.sleep(1)
ip = sys.argv[1]
port = int(sys.argv[2])
start_tcp_client(ip, port)
然后启动两个进程连接不同的数据库
p1
[root@chengqm test]# python same_port_socket.py 10.0.0.247 3306
M
5.7.9-log{De~uf~ÿ?CO,Gdn0{;uumysql_native_password
Xshell
p2
[root@chengqm test]# python same_port_socket.py 10.0.0.248 3306
M
5.7.9-logĕ-4WHKmÿ?TD":+ZHN~&mysql_native_password
查看端口状态