python pymysql re requests socket库的简单运用

要考试了,这里用所学的知识做一个实例

pymysql库

这个库是用来连接数据库的,使用数据库语句在python里创建表和数据库

English.py

import pymysql
def init(): #创建数据库和表
sql_data='create database English'
sql_table='create table word(id int(100),English char(50),Chinese varchar(100),time varchar(50))default charset=utf8;'
DB=pymysql.connect(host='localhost',password='1234',charset='utf8',user='root')
cursor=DB.cursor()
cursor.execute(sql_data)
cursor.execute('use English')
cursor.execute(sql_table)
init()

requests库

通过python来获取网页信息

function.py这个文件来实现单词本的各种功能

function.py

import time
import pymysql
import requests DB = pymysql.connect(host='localhost', password='1234', charset='utf8', user='root', database='English')
cursor = DB.cursor() class Func():
def __init__(self):
self.sql_all = 'select * from word' def id(self): # 获取id值
cursor.execute(self.sql_all)
fin = cursor.fetchall()
if fin:
global id
self.id = fin[-1][0] + 1
else:
self.id = 1 def translate(self):
url='https://fanyi.baidu.com/sug'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0'}
while True:
word = input('请输入被翻译的单词或句子(q==quit)')
if word == 'q':
break
data={'kw':word}
re=requests.post(url=url,headers=headers,data=data)
a=re.json()
for i in a['data']:
print(i['k'] , '\t' , i['v']) def insert(self): # 插入数据
while True:
e = input('请输入英文 (q=quit)')
if e == 'q':
break
c = input('请输入中文')
time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
sql_insert = 'insert into word values(%d,"%s","%s","%s");' % (self.id, e, c, time1)
cursor.execute(sql_insert)
DB.commit()
print(e, c, '以收入数据库')
self.id = self.id + 1 def show(self): # 显示所有单词
cursor.execute(self.sql_all)
fin = cursor.fetchall()
print('序号 英文 中文 时间')
for i in fin:
if i != fin[0]:
print()
for j in i:
print(j,end=' ')
print() def show_something(self):
a = input('请输入以什么为开头的模糊查找')
sql_something = 'select * from word where english like "{0}%"'.format(a)
#print(sql_something)
print('序号 英文 中文 时间')
cursor.execute(sql_something)
a = cursor.fetchall()
for i in a:
if i != a[0]:
print()
for j in i:
print(j,end=' ')
print() def delete(self):
a = int(input('请输入单词序号: '))
sql_del = 'delete from word where id={0} '.format(a)
cursor.execute(self.sql_all)
print('序号 英文 中文 时间')
l = cursor.fetchall()
for i in l:
if i[0] == a:
for j in i:
print(j,end=' ')
print()
#print(sql_del)
cursor.execute(sql_del)
DB.commit() def main():
fun = Func()
fun.id()
print('欢迎来到 <小茅背单词>\n选择功能 1.背单词 2.查翻译 3.显示所有库中单词 4.显示以什么字符开头的单词'
' 5.删除单词 6.开启远程背单词功能 7.q==quit')
while True:
ch = input('请输入序号 (s=show)')
if ch == '1':
fun.insert()
if ch == '2':
fun.translate()
if ch == '3':
fun.show()
if ch == '4':
fun.show_something()
if ch == '5':
fun.delete()
if ch == '6':
import remotely
if ch == 's':
print('欢迎来到 <小茅背单词>\n选择功能 1.背单词 2.查翻译 3.显示所有库中单词 4.显示以什么字符开头的单词'
' 5.删除单词 6.开启远程背单词功能 7.q==quit')
if ch == 'q':
print('Bye')
break main() def rec():
import re
Pattern = re.compile(r"^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)$")
word=input('word')
if Pattern.match(word):
print('成功')

通过bind绑定ip地址使远程的用户可以连接到remotely

remotely.py

import pymysql
import socket
import time
DB = pymysql.connect(host='localhost', password='1234', charset='utf8', user='root', database='English')
cursor = DB.cursor()
class reFunc():
def __init__(self):
self.sql_all = 'select * from word' def id(self): # 获取id值
cursor.execute(self.sql_all)
fin = cursor.fetchall()
if fin:
global id
self.id = fin[-1][0] + 1
else:
self.id = 1 def insert(self): # 插入数据
while True:
e = con.recv(1024).decode('utf-8')
if e == 'q':
s.close()
break
c = con.recv(1024).decode('utf-8')
time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
sql_insert = 'insert into word values(%d,"%s","%s","%s");' % (self.id, e, c, time1)
message = '{0},{1}以收入数据库'.format(e, c)
con.send(message.encode('utf-8'))
cursor.execute(sql_insert)
DB.commit()
print(e, c, '以收入数据库')
self.id = self.id + 1 fun = reFunc()
fun.id() # 先获取id值
s = socket.socket()
s.bind(('10.104.45.223', 8888))
print('已准备')
s.listen(5)
con, address = s.accept()
print('连接成功')
message = ('欢迎来到 <小茅背单词>\n 远程功能,你所输入的数据将会存放在主机库中')
con.send(message.encode('utf-8'))
fun.insert()

socket

这是给远程用户使用的,两个用户连接时首先得相互ping通

客户机.py

import socket
s=socket.socket()
s.connect(('10.104.45.223',8888))
rep=s.recv(1024).decode('utf-8')
print(rep)
def select1():
while True:
message=input('请输入英文 q == quit:')
if message == 'q':
s.send(message.encode('utf-8'))
s.close()
break
else:
s.send(message.encode('utf-8'))
message=input('请输入中文')
s.send(message.encode('utf-8'))
rep=s.recv(1024).decode('utf-8')
print(rep) select1()

Python 英语单词本的更多相关文章

  1. python实现单词本功能

    #实现简单的单词本:# 可以添加单词和词义,当所添加的单词已经存在 让用户知道:# 查找单词,单词不存在时,让用户知道# 删除单词,当删除的单词不存在时,让用户知道# 以上功能无限制操作,直到用户输入 ...

  2. 小白学习Python英语基础差怎么办,都帮你想好拉!看这里

    运算符与随机数 1.module:模块 2.sys(system):系统 3.path:路径 4.import:导入 5.from:从- 定义函数与设定参数 1.birthday:出生日期 2.yea ...

  3. 还在用背单词App?使用Python开发英语单词自测工具,助你逆袭单词王!

    学英语广告 最近也许是刚开学的原因,不管是公众号,还是刷抖音,导出都能看到关于学英语.背单词的广告. 不知道现在学生们背单词买的什么辅导材料.反正我们上学那会,<星火阅读>特别的火.记得当 ...

  4. python百科

    Python 编辑词条 添加义项名 B 添加义项 ? Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第 ...

  5. Python入门教程(3)

    人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...

  6. Python入门教程(2)

    人生苦短,我玩蛇0.0! Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991 ...

  7. Python入门教程(1)

    人生苦短,我用Python! Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于19 ...

  8. Python基础入门教程(4)(数据类型)

    人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...

  9. Python基础入门教程(3)

    人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...

随机推荐

  1. 使用SpringBoot实现登录注册的几个问题

    一.用户名密码都正确的情况下被登录拦截器拦截 控制台报错:org.apache.ibatis.executor.ExecutorException: A query was run and no Re ...

  2. C#多个标题头合并

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.RowType) ...

  3. day9 图书设计项目

    总路由层url from django.conf.urls import url from django.contrib import admin from app01 import views ur ...

  4. Learning Spark中文版--第六章--Spark高级编程(1)

    Introduction(介绍) 本章介绍了之前章节没有涵盖的高级Spark编程特性.我们介绍两种类型的共享变量:用来聚合信息的累加器和能有效分配较大值的广播变量.基于对RDD现有的transform ...

  5. Linux网络管理(一)之配置主机名与域名

    Linux网络管理(一)之配置主机名与域名参考自:[1]修改主机名(/etc/hostname和/etc/hosts区别) https://blog.csdn.net/shmily_lsl/artic ...

  6. 二叉树——Java实现

    1 package struct; 2 3 interface Tree{ 4 //插入元素 5 void insert(int value); 6 //中序遍历 7 void inOrder(); ...

  7. ajaxSubmit返回JSON格式

    开发时遇到根据不同情况返回错误提示信息的需求,用到了ajax中返回json格式数据的. 前台请求代码: <script type="text/javascript">  ...

  8. SQL Server 和 Oracle 以及 MySQL 数据库

    推荐:https://www.zhihu.com/question/19866767 三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Or ...

  9. [BUUCTF]PWN——others_babystack

    others_babystack 附件 步骤: 例行检查,64位程序,开了挺多保护 本地试运行一下程序 64位ida载入,看main函数 1是read函数,存在栈溢出:2是puts函数,3退出 利用思 ...

  10. ASP.NET WebApi 依赖 SAP Connector dll 报错

    说明 本地 VS 开发 ASP.NET WebApi 调试运行没有问题,但发布到服务器 IIS 上就报错.结果发现是 SAP 依赖库的问题:sapnco.dll.sapnco_utils.dll. 错 ...