Python 3共通モジュールのインストール
12788 ワード
1.mysqlドライバ:mysql-connector-python
1.インストール
$ pip3 install mysql-connector-python --allow-external mysql-connector-python
2.導入
1 #!/usr/bin/env python3
2 import mysql.connector
3
4 conn=mysql.connector.connect(user='root',password='password',database='test')
5 cursor=conn.cursor()
6 cursor.execute('create table user(id varchar(20) primary key,name varchar(2))')
7 cursor.execute('insert user (id,name) values(%s,%s)',['1','huangbiao'])
8 print(cursor.rowcount)
9 conn.commit()
10 cursor.close()
11 cursor=conn.cursor()
12 cursor.execute('select * from user where id=%s',['1'])
13 values=cursor.fetchall()
14 print(values)
15 cursor.close()
16 conn.close()
2.ORM:sqlalchemy
1.インストール
$ pip3 install sqlalchemy
2.導入
1 #!/usr/bin/env python3
2 from sqlalchemy import Column,String,create_engine,ForeignKey
3 from sqlalchemy.orm import sessionmaker,relationship
4 from sqlalchemy.ext.declarative import declarative_base
5 """
6 Base=declarative_base()
7 # User ORM
8 class User(Base):
9 __tablename__='user'
10 id =Column(String(20),primary_key=True)
11 name=Column(String(20))
12
13 engine=create_engine('mysql+mysqlconnector://root:526114@localhost:3306/test')
14 DBSession=sessionmaker(bind=engine)
15
16 #
17 session=DBSession()
18 new_user=User(id='1',name='Huangbiao')
19 session.add(new_user)
20 session.commit()
21 session.close()
22 #
23 session=DBSession()
24 user=session.query(User).filter(User.id=='1').one()
25 print('type:',type(user))
26 print('name:',user.name)
27 session.close()
28
29 """
30 # , user book
31 Base=declarative_base()
32 class User(Base):
33 __tablename__='user'
34 id=Column(String(20),primary_key=True)
35 name=Column(String(20))
36 books=relationship("Book")
37 class Book(Base):
38 __tablename__='book'
39 id=Column(String(20),primary_key=True)
40 name=Column(String(20))
41 user_id=Column(String(20),ForeignKey('user.id'))
42 engine=create_engine('mysql+mysqlconnector://root:password@localhost:3306/test')
43 DBSession=sessionmaker(bind=engine)
44 session=DBSession();
45 books=session.query(User).filter(User.id=='1');
46 for data in books:
47 print(data.id)
48 print(data.name)
49 for book in data.books:
50 print('book id:',book.id)
51 print('book name:',book.name)
52 session.close()
3.webフレームワークflask
1.インストール
$ pip3.5 install flask
2.導入
1 #!/usr/bin/env python3
2 from flask import Flask
3 from flask import request
4
5 app=Flask(__name__)
6
7 @app.route('/',methods=['POST','GET'])
8 def home():
9 return '<h1>Home is here</h1>'
10
11 @app.route('/signin',methods=['GET'])
12 def signin_get():
13 return '''<form action='/signin' method='post'>
14 <p>Name:<input type='text' name='user_name'></p>
15 <p>Password:<input type="password" name="password"></p>
16 <p><button type="submit">Sign In</button></p>
17 </form>
18 '''
19 @app.route('/signin',methods=['POST'])
20 def sign_post():
21 if request.form['user_name'].upper()=='ADMIN' and request.form['password']=='admin':
22 return '<h1>Login successfully</h1>'
23 else:
24 return '<h1>Wrong name or password</h1>'
25 if __name__=='__main__':
26 app.run()
4.aiohttp(asyncioによるHTTPフレームワーク)
1.インストール
$ pip3 install aiohttp
2.導入
1 #!/usr/bin/env python3
2 #asyncio TCP,UDP,SSL ,aiohttp asyncio HTTP
3 # aiohttp
4
5
6 # aiohttp http
7
8 import asyncio
9 from aiohttp import web
10 async def index(request):
11 await asyncio.sleep(0.5)
12 return web.Response(body=b'<h1>Index</h1>')
13
14 async def yes(request):
15 await asyncio.sleep(0.5)
16 text='<h1>yes,%s!</h1>'%request.match_info['name']
17 return web.Response(body=text.encode('utf-8'))
18
19 async def init(loop):
20 app=web.Application(loop=loop)
21 app.router.add_route('GET','/',index)
22 app.router.add_route('GET','/yes/{name}',yes)
23 srv=await loop.create_server(app.make_handler(),'121.42.169.254',8000)
24 print('Server started at http://121.42.169.254:80...')
25 return srv
26 if __name__=='__main__':
27 loop=asyncio.get_event_loop()
28 loop.run_until_complete(init(loop))
29 loop.run_forever()
30
5.tkinter(python3.4)
1.インストール
apt-get install libc6
apt-get install libtcl8.6
apt-get install python3-tk-dbg
apt-get install tix