python - Python server that can act as client too
问题描述
I am working on a key manager and i need programs A, B and KM to communicate one with another. What I want is for program A to host connections for B and KM and for B to connect to A and let KM connect to B.
What I want is for program A to host connections for B and KM and for B to connect to A and let KM connect to B. At the moment I create 2 sockets on 2 different ports in program A(one for B and one for KM), I have a socket+port in B which allows KM to connect to it and another port + socket which connects B to A, finally, in KM i connect to both A and B, each connection has a socket and port. My plan is to run A first, then B so that it can connect to A and start listening for KM and then have KM connect to both of them.
code for KM:
TCP_IP = "127.0.0.1"
TCP_PORT_A = 50021
TCP_PORT_B = 50031
sockA = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockA
sockA.connect((TCP_IP, TCP_PORT_A))
print("conn to A")
sockB = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockB.connect((TCP_IP, TCP_PORT_B))
print("conn to B")
code for A:
TCP_IP = "127.0.0.1"
TCP_PORT_REC_B = 50001
TCP_PORT_REC_KM = 50021
sockB = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockB.bind((TCP_IP, TCP_PORT_REC_B))
sockB.listen(1)
print('listening on ', (TCP_IP, TCP_PORT_REC_B))
connB, addrB = sockB.accept()
print("connection address B: ", addrB)
sockKM = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockKM.bind((TCP_IP, TCP_PORT_REC_KM))
sockKM.listen(1)
print('listening on ', (TCP_IP, TCP_PORT_REC_KM))
connKM, addrKM = sockKM.accept()
print("connection address K: ", addrKM)
code for B:
TCP_IP = "127.0.0.1"
TCP_PORT_A = 50001
TCP_PORT_KM = 50031
sockA = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockA.connect((TCP_IP, TCP_PORT_A))
print("connected to A")
sockKM = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockKM.bind((TCP_IP, TCP_PORT_KM))
print("listening")
sockKM.listen(1)
connKM, addrKM = sockKM.accept()
print("connection address k: ", addrKM)
I run A, then B, B says that it connected to A and starts listening, A doesn't confirm the connection, I run KM and i get:
sockA.connect((TCP_IP, TCP_PORT_A))
ConnectionRefusedError: [WinError 10061] No connection could be made
because the target machine actively refused it
解决方案
Your problem is right here:
sockA = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Everyone of your sockets use SOCK_STREAM
except for this one. It's in "code for B". Also you have a line that just says sockA
in "code for KM".
EDIT: Should work the way you intended if you fix that problem. I just tested it myself.
推荐阅读
- stripe-payments - 从 Stripe 返回“您不能在 FI 中指定不同的服务协议”
- c++ - Linux 和 Mac 中是否有等效的 AddDllDirectory?
- android - Braintree 信用卡/借记卡集成不起作用
- android - Flutter usb 权限问题 - 提示不出现 - flutter_usb_write
- python - 如何从文件创建一个大字典?
- spring - 如何从 ui 访问 google 项目,而不是从 Eclipse 控制台获取链接
- android - 回收站视图滑动手势 - 两种状态
- python - 计算 pandas 数据帧值的标准偏差
- kotlin - Kotlin 输入名字和姓氏
- css - 如何使用antd react js制作3个嵌套表