First, PYTHON-MYSQL Environment preparation:
Windows version:
1. Install MySQL
2, install the Python operation MySQL module, http://www.codegood.com/archives/129
3. Install MySQL GUI
4, Verification: Import MySQLdb success
Linux version:
1. Install MySQL
Reference: http://jingyan.baidu.com/article/a378c9609eb652b3282830fd.html
Note:./scripts/mysql_install_db--user=mysql--basedir=/usr/local/mysql/--datadir=/usr/local/mysql/data/mysql
2, install the Python Operation MySQL module, mysql-python-1.2.5.tar.gz
3, Verification: Import MySQLdb success
Wu Jianzi Blog : http://www.cnblogs.com/wupeiqi/articles/4938499.html
Second, Python-mysql-api
Mysql:http://www.cnblogs.com/wupeiqi/articles/5095821.html
1. Query data
>>> Import MySQLdb
>>>
>>> conn = MySQLdb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' root ', db= ' swconfig ')
>>> cur = conn.cursor ()
>>> recount=cur.execute (' select * from Devinfors ')
>>> recount=cur.execute (' select * from Devinfors ')
>>> Data=cur.fetchall ()
>>> Print Data
((1L, ' bf50sw19-b3 ', ' 80.97.35.19 ', ' 101 ', ' 102 ', ' bf50sw20-b3 ', ' 80.97.35.20 ', ' 101 ', ' 102 ', ' B3 ', ' 11D ', ' A3 ', ' ", 0L) , (2L, ' bf50sw03-b5 ', ' 80.97.3.19 ', ' 107 ', ' 108 ', ' bf50sw04-b5 ', ' 80.97.3.20 ', ' 107 ', ' 108 ', ' B3 ', ' 21F ', ' B2 ', ' to ', ' 0L '), (3L, ' bf50sw21-b3 ', ' 80.97.35.21 ', ' 103 ', ' 104 ', ' bf50sw22-b3 ', ' 80.97.35.22 ', ' 103 ', ' 104 ', ' B3 ', ' 21B ', ' C4 ', ' it ', 0L) , (7L, ' bf50sw19-b3 ', ' 80.97.35.19 ', ' a ', ' 106 ', ' bf50sw20-b3 ', ' 80.97.35.20 ', ' a ', ' 106 ', ' 11D ', ' B3 ' ' ), (9L, ' bf50sw21-b3 ', ' 80.97.35.21 ', ' 107 ', ' 108 ', ' bf50sw22-b3 ', ' 80.97.35.22 ', ' 107 ', ' 108 ', ' B3 ', ' 21A ', ' D2 ', ' 07 ', 0 L), (10L, ' bf50sw21-b3 ', ' 80.97.35.21 ', ' 109 ', ' a ', ' bf50sw22-b3 ', ' 80.97.35.22 ', ' 109 ', ' a ', ' B3 ', ' 21A ', ' D2 ', ' 07 ', 0L))
>>> Print Recount
6
>>> Cur.close ()
>>> Conn.close ()
2. Inserting data
>>> conn = MySQLdb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' root ', db= ' swconfig ')
>>> Cur=conn.cursor ()
>>> sql= "INSERT into Vlaninfos values (1,%s,%s,%s)"
>>> params= ("80.2.238.0", "802", "B3")
>>> Recount=cur.execute (Sql,params)
>>> Conn.commit ()
3. Modify the data
>>> sql= "Update Vlaninfos set vlan=%s,vlanid=%s where id=1"
>>> params= ("80.4.238.0", "800")
>>> Recount=cur.execute (Sql,params)
>>> Conn.commit ()
4. Delete data
>>> sql= "Delete from Vlaninfos where id=%s"
>>> params= (1,)
>>> Recount=cur.execute (Sql,params)
>>> Conn.commit ()
Third, socket
Http://www.cnblogs.com/wupeiqi/articles/5040823.html
[email protected] demo]# cat server.py
#!/usr/bin/python27
#coding: Utf-8
Import socket
S=socket.socket ()
ip_port= (' 127.0.0.1 ', 8888)
S.bind (Ip_port)
S.listen (5)
While True:
Conn,address=s.accept ()
Conn.send (' Hello. ')
Flag=true
While flag:
DATA=CONN.RECV (1024)
Print data
If data== ' exit ':
Flag=false
Conn.send (' Welcome to Python socket ')
Conn.close ()
[email protected] demo]# cat client.py
#!/usr/bin/python27
#coding: Utf-8
Import socket
Client=socket.socket ()
ip_port= (' 127.0.0.1 ', 8888)
Client.connect (Ip_port)
While True:
DATA=CLIENT.RECV (1024)
Print data
Input_str=raw_input (' Clent: ')
Client.send (INPUT_STR)
If input_str== ' exit ':
Break
[Email protected] demo]#
Python-mysql,socket