python程序多线程运行_在同一程序中运行的Python多线程服务器

所以我现在正在为一个小游戏编写一个python服务器。我目前正在尝试让程序在线程中运行两个服务器。通过测试,我发现在我的进程中有一个小问题,那就是一次只有一个服务器在运行。我为每台服务器使用不同的端口,但是首先连接的服务器占主导地位。

服务器代码段:class loginServer(socketserver.BaseRequestHandler):

def handle(self):

# self.request is the TCP socket connected to the client

self.data = self.request.recv(1024).strip()

sent = str(self.data,'ascii').split(":")

print("login:\t\t",sent)

if (sent[0] == "l"):

#login check

#cur.execute("SELECT password, userid FROM `prototb` WHERE username = '"+sent[1]+"'")

cur.execute("SELECT `displayname`,`curmap`,`curpos`,`Inventory`,`abilities`,`password`,`userid` FROM `prototb` WHERE username = '"+sent[1]+"'")

plyInfo = cur.fetchone()#get the information from the database

if (plyInfo != None):#if the account doesnt exist the username is wrong

if (plyInfo[5] == sent[2]):#check the correctness of the password

print("Login-Success:\t",plyInfo)

send = pickle.dumps([plyInfo[1],plyInfo[2],plyInfo[3],plyInfo[4],plyInfo[5]])

else:

self.request.send(bytes("-", 'ascii'))#refuse entry (bad password)

else:

self.request.send(bytes("-", 'ascii'))#refuse entry (bad username)

def run_loginServer():

HOST, PORT = "localhost", 9000

# Create the server, binding to localhost on port 9000

server = socketserver.TCPServer((HOST, PORT), loginServer)

# Activate the server; this will keep running until you

# interrupt the program with Ctrl-C

server.serve_forever()

对于另一个服务器线程:

^{pr2}$

然后我运行这个来启动它们:if __name__ == "__main__":

#get the database handle

run_dbConnect()

#run the map pod server

run_podLauncher()

#run the login server ### localhost 9000

run_loginServer()

当我单独地注释掉线程时,客户机是单独地连接到它们的,但永远不会在一起。任何帮助将不胜感激。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值