import socket
import threading
import os
import time
import MySQLdb
import types
db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='python')
cur=db.cursor()
HOST='localhost'
PORT=21217
OnlineClientList=[]
CurrentUserList=[]
messageSendList=['','','']
def register(clientsocket):
clientsocket.sendall('input username and password:')
while 1:
try:
newUserName=clientsocket.recv(1024)#protect datebase from sql injection
print 'success'
break
except:
continue
while 1:
try:
newUserPasswd=clientsocket.recv(1024)
break
except:
continue
print newUserName,newUserPasswd
insertSql="INSERT INTO user (user,passwd) VALUES ('"+newUserName+"','"+newUserPasswd+"');"
print insertSql
n=cur.execute(insertSql)
db.commit()
print 'insert success'
return 1
def loginCheck(username,password):
selectSql="select passwd from user where user='"+username+"';"
n=cur.execute(selectSql)
for row in cur.fetchall():
for result in row:
if password == result:
return 1
else:
return 0
def StringAddVar(string,var):
return string+var
def StringAddVarAddString(string1,var,string2):
return string1+var+string2
'''def recvThread(clientsocket,peerIDPosition):
global messageSendList
while 1:
try:
messageSendList[peerIDPosition]=clientsocket.recv(1024)
except:
continue
def sendThread(clientsocket,selfIDPosition):
global messageSendList
while 1:
if messageSendList[selfIDPosition]=='':
continue
clientsocket.sendall(messageSendList[selfIDPosition])
messageSendList[selfIDPosition]=''
'''
def makeUserList(OnlineClientList,CurrentUserList):
userList='\n'
for i in range(len(OnlineClientList)):
print 'i=:',i
userListTemp=OnlineClientList[i]
userList=userList+userListTemp[0]+'\n'
if CurrentUserList.__contains__(userListTemp[0]):
continue
CurrentUserList.append(userListTemp[0])
return CurrentUserList,userList
def ClientThreadInit(clientsocket):
global OnlineClientList,CurrentUserList
(clientAddr,clientPort)=clientsocket.getpeername()
clientsocket.sendall("please input your userID and passwd..:")
print "input your user ID"
while 1:
while 1:
try:
userID=clientsocket.recv(1024) #access control need connect to mysql later
print 'userID is:'+userID
break
except:
continue
while 1:
try:
passwd=clientsocket.recv(1024) #access control need connect to mysql later
print 'password is:'+passwd
break
except:
continue
###login check
if loginCheck(userID,passwd):
clientsocket.sendall('correct')
print 'login success'
break
else:
clientsocket.sendall('incorrect')
print 'login fail'
OnlineClientList.append([userID,clientAddr,clientPort])
#need access control
print OnlineClientList
CurrentUserList,userList=makeUserList(OnlineClientList,CurrentUserList)
#need to make a refresh function to timely print current userlist,for eg:write a function contain sendall user
#need to translate list to string or buffer
print 'currentlist',CurrentUserList
UserNum=len(CurrentUserList)
#add a waiting function
clientsocket.sendall(userList)
if len(CurrentUserList)>1:
clientsocket.sendall('There is more than 2 user online')
time.sleep(1)
clientsocket.sendall('please choose the user you want to talk with:')
else:
clientsocket.sendall("There's 0 people online except you,please wait...")
while(1):
if len(CurrentUserList)>UserNum:
CurrentUserList,userList=makeUserList(OnlineClientList,CurrentUserList)
clientsocket.sendall(StringAddVarAddString('new user is online,current list:',userList,'please choose the user you want to talk with:'))
break
while 1:
try:
choseUserName=clientsocket.recv(1024)
break
except:
continue
print 'print current List',CurrentUserList
print '\n',choseUserName #if possible,here coule use mysql to select
return choseUserName,userID
def UserIndexSearch(UserID):
global CurrentUserList
return CurrentUserList.index(UserID)
def ClientThread(clientsocket):
global messageSendList
branchJudge=''
connPeer=''
selfID=''
clientsocket.setblocking(0)
print "create a clientthread",threading.currentThread().getName()
print "got connection from",clientsocket.getpeername()
clientsocket.sendall('Do you need to register a new username?')
while 1:
try:
branchJudge=clientsocket.recv(1024)
break
except:
continue
print type(int(branchJudge))
if branchJudge == '1':
print 'register'
print register(clientsocket)
connPeer,selfID=ClientThreadInit(clientsocket)
else:
print '???'
connPeer,selfID=ClientThreadInit(clientsocket)
print connPeer
peerIDPosition=UserIndexSearch(connPeer)
selfIDPosition=UserIndexSearch(selfID)
print 'peer position:',peerIDPosition
print 'self positon:',selfIDPosition
'''rThread=threading.Thread(target=recvThread,args=[clientsocket,peerIDPosition])
sThread=threading.Thread(target=sendThread,args=[clientsocket,selfIDPosition])
rThread.setDaemon(1)
sThread.setDaemon(1)
rThread.start()
sThread.start()
'''
while 1:
if messageSendList[selfIDPosition] !='':
clientsocket.sendall(messageSendList[selfIDPosition])
messageSendList[selfIDPosition]=''
try:
messageSendList[peerIDPosition]=clientsocket.recv(1024)
except:
continue
clientsocket.close()
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(3)
while 1:
conn,addr=s.accept()
t=threading.Thread(target=ClientThread,args=[conn])
t.setDaemon(1)
t.start()