Demand:
1. Development of Duplex chat software with socket, server interaction with Client
2, chat records to log to the MySQL database
3. Clients can view chat history
4, with Three-tier architecture development
Program Architecture
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/2D/wKiom1e2ycryOFhWAAAplDAuF1E935.png-wh_500x0-wm_3 -wmp_4-s_991633550.png "title=" qq picture 20160819165629.png "alt=" wkiom1e2ycryofhwaaapldauf1e935.png-wh_50 "/>
Analysis of the functions of each file
-model model Layer
-dialogue.py Dialoue Table
-users.py Users Table
-utility Public Floor
-sql_helper.py Public configuration file
-client.py Client File
-conf.py configuration file
-index.py Main Program Files
-server.py server-side Files
Sql_helper file of public configuration
#cat sql_helper.py#!/usr/bin/env python#coding:utf-8import MySQLdbimport confclass Mysqlhelper (object): def __init__ (self): self.__conn_dict = conf.conf_dict self.__t1_ dict = conf.t1_dict #插入数据 def get_dict (self,sql,params): Conn = mysqldb.connect (**self.__t1_dict) cur = conn.cursor () reCount = Cur.execute (sql,params) conn.commit () cur.close () conn.close () #return reCount #查询用户名和密码 def get_one (self,sql, params): conn = mysqldb.connect (**self.__conn_dict) cur = conn.cursor () recounts = cur.execute (sql,params) data = cur.fetchone () cur.close () conn.close () return data #查询聊天记录 def get_all (self,sql,params): conn = mysqldb.connect (**self.__t1_dict) cur = conn.cursor () recounts = cur.execute (sql,params) data = cur.fetchall () Cur.close () conn.close () return data
The users table of the model layer
#cat users.py#!/usr/bin/env python#coding:utf-8from utility.sql_helper Import mysqlhelper class users (object): def __ init__ (self): self.__helper = mysqlhelper () #查询命令 def checkvalidate (self,username,password): sql = "select * from user where user =%s and password =%s" params = (username,password) return self . __helper. Get_one (sql,params)
Dialogue Table of the model layer
#cat dialogue.py#!/usr/bin/env python#coding:utf-8from utility.sql_helper import MySQLHelper #把类导入进来class inserts (object): def __init__ (self): self.__helper = mysqlhelper () #插入数据SQL语句 def checkinsert (self,name,time,dialogue): sql = "INSERT INTO T1 (name,time,dialogue) values (%s,%s,%s)" params = (name,time,dialogue) return self.__helper. Get_dict (sql,params) #查询聊天记录SQL语句 def showdialogue (self,name,names): sql = "select * from t1 where name = %s or name = %s " params = (name,names) return self.__helper. Get_all (sql,params)
Configuration file
#cat conf.py#!/usr/bin/env python#coding:utf-8conf_dict = dict (host= ' 192.168.1.109 ', user= ' root ', passwd= ' 123456 ', db = ' MySQL ') t1_dict = dict (host= ' 192.168.1.109 ', user= ' root ', passwd= ' 123456 ', db= ' Wsyht ')
Service-side files
# cat server.py#!/usr/bin/env python#coding:utf-8def startsocket (): from model.dialogue import Inserts import socket inserts = inserts () sk = socket.socket () ip_port = (' 127.0.0.1 ', 9915) sk.bind (ip_port) sk.listen (5) #阻塞数量 while true: conn,address = sk.accept () conn.send (' Hello ') print ' New user enters system ' print ' Server:hello ' flag = True &nbsP;while flag: import time             DATA = CONN.RECV (1024x768) if data == ' Exit ': flag = False print ' The other party has exited the system ' break print ' Client: ',data inp = raw_input (' server: ') &Nbsp; conn.send (inp) name = ' Server ' time = time.strftime ('%Y-%m- %d %h:%m:%s ') result = Inserts. Checkinsert (name,time,inp) conn.close ()
Client Files
#cat clieny.py#!/usr/bin/env python#coding:utf-8import socketclient = socket.socket () ip_port = (' 127.0.0.1 ', 9915) client.connect (ip_port) print "enter 1 To view chat history" while true: from model.dialogue import inserts import time inserts = inserts () data = client.recv (1024 ) print ' server: ', data inp = raw_input (' client: ') client.send (inp) if inp == ' 1 ': print ' Dn al ' from model.dialogue import inserts inserts = inserts () &nbsP; result = inserts. Showdialogue (' Server ', ' client ') print result import time name = ' client ' time = time.strftime ('%y-%m-%d %h:%m:%s ') result = inserts. Checkinsert (name,time,inp) if inp == ' exit ': break
Main Program Files
#!/usr/bin/env python#coding:utf-8from model.users Import usersimport serverdef main (): user = Raw_input (' username: ') PWD = raw_input (' password: ') users = users () result = Users. Checkvalidate (user,pwd) If not Result:print ' you entered the username or password incorrectly ' else:print ' Welcome to Login Admin Page ' Server. Startsocket () If __name__ = = ' __main__ ': main ()
To perform the test:
1) service-side Execution
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/86/2D/wKioL1e20NfQ65FMAAA98y7Lmxs543.png-wh_500x0-wm_3 -wmp_4-s_1692475895.png "title=" qq picture 20160819172217.png "alt=" wkiol1e20nfq65fmaaa98y7lmxs543.png-wh_50 "/>
2) Client Execution
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/86/2D/wKioL1e20JSxmkDjAAA5ZzzI9BA773.png-wh_500x0-wm_3 -wmp_4-s_3393389477.png "title=" 2.png "alt=" wkiol1e20jsxmkdjaaa5zzzi9ba773.png-wh_50 "/>
3) service-side Execution
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/2D/wKiom1e20J_QD43_AAA0ZBW2-ZM848.png-wh_500x0-wm_3 -wmp_4-s_2039399043.png "title=" 3.png "alt=" wkiom1e20j_qd43_aaa0zbw2-zm848.png-wh_50 "/>
4) Client Execution
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/86/2D/wKioL1e20KuyeYxhAAAukMhpfSI759.png-wh_500x0-wm_3 -wmp_4-s_172682802.png "title=" 4.png "alt=" wkiol1e20kuyeyxhaaaukmhpfsi759.png-wh_50 "/>
5) service-side Execution
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/86/2D/wKioL1e20TfyJChrAAAydcveLgM568.png-wh_500x0-wm_3 -wmp_4-s_123540527.png "title=" 5.png "alt=" wkiol1e20tfyjchraaaydcvelgm568.png-wh_50 "/>
6) Client View Chat history
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/86/2E/wKiom1e20c7ilMhPAACh95TS40s697.png-wh_500x0-wm_3 -wmp_4-s_562801735.png "title=" 6.png "alt=" wkiom1e20c7ilmhpaach95ts40s697.png-wh_50 "/>
7) View Database tables
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/86/2E/wKiom1e20g-T6yHCAAA1HStyyU8129.png-wh_500x0-wm_3 -wmp_4-s_549589232.png "title=" 7.png "alt=" wkiom1e20g-t6yhcaaa1hstyyu8129.png-wh_50 "/>
This article is from the "operations notes" blog, make sure to keep this source http://wsyht90.blog.51cto.com/9014030/1840375
Python Development Duplex Chat software