问题
I am using socket module in python to create two clients and one server. Client_1 shall send content of a .txt file to Client_2, through the server. In server I am using thread module to have a multiple client support.
I have created three Threads in my server:-
1) First thread handles the client connections 2) Second thread receives the contents of the .txt file from Client_1 3) Third thread sends this contents to Client_2
The problem that I am facing, while executing the above mentioned server, Client_1 and Client_2, The contents of .txt file is successfully sent from Client_1 to server. But, The server is not able to send this contents to Client_2.
This may be because the third thread is not able to get the contents received in second thread (but it is what I think, I may be wrong). Below I am sharing all the three codes along with the .txt file that I am using.
server code
import socket
import threading
class ClientThread(threading.Thread): ############ this thread manages the client connections ############
def __init__(self,conn,Address):
threading.Thread.__init__(self)
self.conn = conn
print("New connection added", Address)
def run(self):
print ("Connection from : ", Address)
self.conn.send(bytes("You are connected to IIT H",'utf-8'))
class RecieveThread(threading.Thread): ############# this thread takes care about recieving the contents of the .txt file
def __init__(self,conn,Address): ## from ####### Client_1 ###########################
threading.Thread.__init__(self)
self.conn = conn
def run(self):
print("This data is from Housekeeping unit: ",Address)
data = self.conn.recv(1024).decode()
print("The wastebin attributes are: ", data)
class SendThread(threading.Thread):
def __init__(self,conn,Address):
threading.Thread.__init__(self) ##### This thread takes care about sending the the recieved contents to ## Client_2 #####
self.conn = conn
def run(self):
data = self.conn.recv(1024).decode()
print("Sending this data to mobile unit")
self.conn.send(data.encode())
host = '' ######### Getting the hostname #######
port = 5000 ###### Fixing the port ##########
server_socket = socket.socket() ######### initiate the socket #########
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ###### Allows to bind to exacly the same port and IP address#####
server_socket.bind((host, port)) ###### binding the host and the port #######
print("The IIT-H server is active")
print("Waiting for clients to connect")
while True:
server_socket.listen(4) ###### allowing only 4 clients to be in queue to connect ######
conn, Address = server_socket.accept() ###### accepting the connections #########
ClientThread(conn,Address).start() ##### Calling the first thread class #####
RecieveThread(conn,Address).start() ###### Calling the second thread class ########
SendThread(conn,Address).start() ###### Calling the third thread classs ##########
conn.close() #### closing the connection ######
The Client_1 code
import socket
host = socket.gethostname() ############### as both code is running on same pc ###################3
port = 5000 ############# socket server port number################33
client_socket = socket.socket() ################# initiate the connection ##########e
client_socket.connect((host, port))
in_data = client_socket.recv(1024).decode() ############### connect to the server########################
print("From IIT-H server: ", in_data)
wastebin_attr = open("/Users/sayantanmandal/Python Programs/Waste_data.txt", 'r') ######### Opening the .txt file ########
wa = wastebin_attr.read() ############ Reading the .txt file ##################
message = wa
client_socket.send(message.encode()) ########## sending the message from socket point but before encoding in bytes #########
client_socket.close() ################### closing the connection ######################
The Client_2 code
import socket
host = socket.gethostname()
############### as both code is running on same pc ###################3
port = 5000 ############# socket server port number################33
client_socket = socket.socket() ################# initiate the connection ##########e
client_socket.connect((host, port)) ############### connect to the server########################
in_data = client_socket.recv(1024).decode()
print("From IIT-H server: ", in_data)
data = client_socket.recv(1024).decode()
print("From Housekeeping unit: ", data)
client_socket.close() ################### closing the connection ######################
The .txt file that I am using
1. Wastebin serial number:
2. Wastebin type:
3. Wastebin status:
4. Wastebin action:
5. Wastebin zone:
6. Wastebin Lat./Long. :
Please help me as to how I can overcome this situation. Thank you all.
来源:https://stackoverflow.com/questions/61357286/how-to-pass-objects-from-one-thread-class-to-another-thread-class-in-python