亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python實現電子詞典

瀏覽:10日期:2022-08-04 15:54:52

本文實例為大家分享了python實現電子詞典的具體代碼,供大家參考,具體內容如下

服務端

#!/usr/bin/env python3from __future__ import unicode_literals# coding=utf-8from socket import *import osimport pymysqlimport timeimport sysimport signalDICT_TEXT = './dict.txt'HOST = ’0.0.0.0’PORT = 8000ADDR = (HOST, PORT)# 主控制流程def main(): # 連接數據庫 db = pymysql.connect (’localhost’, ’root’, ’123456’, ’dict’) # 創建流式套接字 s = socket() s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) s.bind(ADDR) s.listen(5) # 或略子進程退出 signal.signal(signal.SIGCHLD, signal.SIG_IGN) while True: try: c, addr = s.accept() print('Connect from', addr) except KeyboardInterrupt: s.close() sys.exit('服務器退出') except Exception as e: print(e) continue # 創建子進程處理客戶端請求 pid = os.fork() if pid == 0: s.close() do_child(c, db) else: c.close()def do_child(c, db): # 循環接收請求 while True: data = c.recv(128).decode() print('Request:', data) if (not data) or data[0] == ’E’: c.close() sys.exit(0) elif data[0] == ’R’: do_register(c, db, data) elif data[0] == 'L': do_login(c, db, data) elif data[0] == ’Q’: do_query(c, db, data) elif data[0] == ’H’: do_history(c, db, data)def do_register(c, db, data): l = data.split(’ ’) name = l[1] passwd = l[2] cursor = db.cursor() sql = 'select * from user where name=’%s’' % name cursor.execute(sql) r = cursor.fetchone() if r != None: c.send(b’EXISTS’) return sql = 'insert into user (name,passwd) values (’%s’,’%s’)' % (name, passwd) try: cursor.execute(sql) db.commit() c.send(b’OK’) except: db.rollback() c.send(b’FALL’) return else: print('%s注冊成功' % name)def do_login(c, db, data): l = data.split(’ ’) name = l[1] passwd = l[2] cursor = db.cursor() sql = 'select * from user where name=’%s’ and passwd=’%s’' % (name, passwd) cursor.execute(sql) r = cursor.fetchone() if r == None: c.send(’用戶名或密碼不正確’.encode()) else: c.send(b’OK’)def do_query(c, db, data): l = data.split(’ ’) name = l[1] word = l[2] cursor = db.cursor() def insert_history(): tm = time.ctime() sql = 'insert into hist (name,word,time) values (’%s’,’%s’,’%s’)' % (name, word, tm) try: cursor.execute(sql) db.commit() except: db.rollback() return try: f = open(DICT_TEXT, ’rb’) except: c.send('500 服務端異常'.encode()) return while True: line = f.readline().decode() w = line.split(’ ’)[0] if (not line) or w > word: c.send('沒找到該單詞'.encode()) break elif w == word: c.send(b’OK’) time.sleep(0.1) c.send(line.encode()) insert_history() break f.close()def do_history(c, db, data): name = data.split(’ ’)[1] cursor = db.cursor() try: sql = 'select * from hist where name=’%s’' % name cursor.execute(sql) r = cursor.fetchall() if not r: c.send(’沒有歷史記錄’.encode()) return else: c.send(b’OK’) except: c.send('數據庫查詢錯誤'.encode()) return n = 0 for i in r: n += 1 # 最多顯示10條 if n > 10: break time.sleep(0.1) msg = '%s %s %s' % (i[1], i[2], i[3]) c.send(msg.encode()) time.sleep(0.1) c.send(b’##’)if __name__ == '__main__': main()

客戶端

#!/usr/bin/env python3#coding=utf-8from socket import *import sys import getpassdef main(): if len(sys.argv) < 3: print('argv is error') return HOST = sys.argv[1] PORT = int(sys.argv[2]) ADDR = (HOST,PORT) s = socket() s.connect(ADDR) while True: print(’’’n ===========Welcome========= --1.注冊 2.登錄 3.退出-- =========================== ’’’) try: cmd = int(input('輸入選項>>')) except Exception: print('輸入命令錯誤') continue if cmd not in [1,2,3]: print('對不起,沒有該命令') sys.stdin.flush() #清除輸入 continue elif cmd == 1: name = do_register(s) if name != 1: print('注冊成功,直接登錄!') login(s,name) else: print('注冊失敗!') elif cmd == 2: name = do_login(s) if name != 1: print('登錄成功!') login(s,name) else: print('登錄失敗!') elif cmd == 3: s.send(b'E') sys.exit('謝謝使用')def do_register(s): while True: name = input('用戶名:') passwd = getpass.getpass('密 碼:') passwd1 = getpass.getpass('確認密碼:') if (’ ’ in name) or (’ ’ in passwd): print('用戶名密碼不允許空格') continue if passwd != passwd1: print('兩次密碼不一致') continue msg = 'R {} {}'.format(name,passwd) #發送請求 s.send(msg.encode()) #接收回復 data = s.recv(128).decode() if data == 'OK': return name elif data == ’EXISTS’: print('該用戶已存在') return 1 else: return 1def do_login(s): name = input('用戶名:') passwd = getpass.getpass('密 碼:') msg = 'L {} {}'.format(name,passwd) s.send(msg.encode()) data = s.recv(128).decode() if data == ’OK’: return name else: print(data) return 1def login(s,name): while True: print(’’’n ===========查詢界面============ 1.查詞 2.歷史記錄 3.注銷 ============================= ’’’) try: cmd = int(input('輸入選項>>')) except Exception: print('命令錯誤') continue if cmd not in [1,2,3]: print('對不起,沒有該命令') sys.stdin.flush() #清除輸入 continue elif cmd == 1: do_query(s,name) elif cmd == 2: do_history(s,name) elif cmd == 3: returndef do_query(s,name): while True: word = input('單詞:') if word == '##': break msg = 'Q {} {}'.format(name,word) s.send(msg.encode()) data = s.recv(128).decode() if data == ’OK’: data = s.recv(2048).decode() print(data) else: print(data)def do_history(s,name): msg = 'H {}'.format(name) s.send(msg.encode()) data = s.recv(128).decode() if data == ’OK’: while True: data = s.recv(1024).decode() if data == '##': break print(data) else: print(data)if __name__ == '__main__': main()

插入字典

import pymysql import ref = open(’dict.txt’)db = pymysql.connect(’localhost’,’root’,’123456’,’dict’)cursor = db.cursor()for line in f: try: l = re.split('[ ]+',line) except: pass sql = 'insert into words (word,interpret) values (’%s’,’%s’)'%(l[0],’ ’.join(l[1:])) try: cursor.execute(sql) db.commit() except: db.rollback() f.close()

python實現電子詞典

python實現電子詞典

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 免费jizz在线播放视频 | 国产精品黄在线观看观看 | 一区二区在线 | 午夜视频黄色 | 色婷婷影院在线视频免费播放 | 国内精品免费视频自在线 | 日本多人激情免费视频 | 亚洲成人一区 | 中文字幕亚洲第一 | 亚洲国产第一区二区香蕉 | 欧美国产高清 | 国产精品品福利视频 | 免费观看黄色大片 | 国产成人资源 | 日本巨乳中文字幕 | 成人免费视频无遮挡在线看 | 超级极品白嫩美女在线 | 免费看三级黄色片 | 亚洲一级毛片在线观播放 | 92午夜影院| 亚洲精品免费在线观看 | 欧美一级黄色带 | 一级特黄录像免费播放中文 | 一区二区免费在线观看 | 国产成人综合洲欧美在线 | 久久亚洲国产成人亚 | 国产精品久久久久久一区二区三区 | 成人免费视频一区二区 | 免费成人 | 我要看黄色录像一级片 | 免费一级a毛片在线搐放正片 | 在线黄色免费观看 | 色亚洲色图 | 黄色福利小视频 | 亚洲精品日韩精品一区 | 欧美精品亚洲精品日韩专区 | 日韩视频不卡 | 日本久久综合网 | 免费黄色国产视频 | 中文字幕在线看片成人 | 国产成人毛片毛片久久网 |