В этом коде, когда я пытался без применения многопоточности на сокете python, он работает отлично. Но после использования многопоточности для параллелизма первый, в то время как l oop работает нормально, но во втором, когда l oop, он принимает его как 2-й поток, который не завершает процедуру отправки ключа доступа к android. Здесь я не хочу 2-й l oop как 2-й поток. Как я это сделаю? Любая помощь будет оценена!
import mysql.connector as mysql
import socket
import sys
import json
import threading
mydb1=mysql.connect(
user = 'rajat',
passwd = 'rajat',
host = 'localhost',
database = 'master_database'
)
class ClientThread(threading.Thread):
def __init__(self,clientAddress,clientsocket):
threading.Thread.__init__(self)
self.csocket = clientsocket
self.addr = clientAddress
print ("New connection added: ", clientAddress)
def run(self):
print ("Connection from : ", self.addr)
#self.csocket.send(bytes("Hi, This is from Server..",'utf-8'))
msg = ''
while True:
#csocket, clientAddress = s.accept()
#print ('Connect with ' + addr[0] + ':' + str(addr[1]))
df7 = self.csocket.recv(1024)#Receiving the data in df7
df8 = json.loads(df7)
df2 = list(df8.values())
mycursor4=mydb1.cursor()
mycursor4.execute("SELECT bar_code_no FROM form_simpleform")
obj1 = mycursor4.fetchone()
qr_obj=print(obj1[0])
mycursor3 = mydb1.cursor()
mycursor3.execute("SELECT bar_code_no FROM bar_code")
obj2 = mycursor3.fetchone()
bar_obj=print(obj2[0])
if qr_obj == bar_obj:
print("This bar code has a existence in the database.")
else:
print("This bar code does not exist.")
mycursor1=mydb1.cursor()
mycursor1.execute("SELECT * FROM form_simpleform WHERE id=1")
df3=mycursor1.fetchone()
if df2 == (list(df3)):
print('Data of QR Code also Exists')
mycursor2 = mydb1.cursor()
mycursor2.execute("SELECT * FROM android_display_data")
df4 = mycursor2.fetchone()
self.csocket.send(str(df4).encode('utf-8'))
print("Data(Name, Email_id, Phone_No) is sent to the client ")
self.csocket.close()
message=[]
while True:
clientsock, clientAddress = s.accept()
message = self.csocket.recv(1024).decode('ISO-8859-1')
print("Received new message from client")
op=((message).split(","))
print(op)
d = dict(s.split(':') for s in op)
yo = print(list(d.values()))
mycursor5 = mydb1.cursor()
sql2 = ("INSERT INTO imeiserial (IMEI, SimSerialNumber) VALUES (%s, %s)")
result = mycursor5.execute(yo, sql2)
print("Data inserted into bar_code Database")
pass_key = '90i5n4r16191'
self.csocket.send(str(pass_key).encode('utf-8'))
print(" Passkey send to Android ")
break
else:
print('Invalid Data')
break
print('Connection Closed!!')
#s.close()
LOCALHOST = "192.168.0.121"
PORT = 8011
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((LOCALHOST, PORT))
print("Server started")
print("Waiting for client request..")
while True:
s.listen(5)
clientsock, clientAddress = s.accept()
newthread = ClientThread(clientAddress, clientsock)
newthread.start()
Вывод, который я получаю -
Server started
Waiting for client request..
Connection from : ('192.168.0.108', 42762)
456456
456456
This bar code has a existence in the database.
Data of QR Code also Exists
Data(Name, Email_id, Phone_No) is sent to the client
Connection from : ('192.168.0.108', 42764)
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Pallavai\Nox_share\AnacondaNEW\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "server-side2.py", line 34, in run
df8 = json.loads(df7)