python-mysql安装:
apt-get install python-pipapt-get install libmysqlclient-devpip install mysql-python
------------------------------------python mysql交互--------------------------------------- #查询:import MySQLdbtry: conn=MySQLdb.connect(host='10.86.10.21',user='root',passwd='mysql',db='python',port=3306) cur=conn.cursor() cur.execute('select * from test111') print cur.fetchall() #取全部行 print cur.fetchmany(5) #取前5行 cur.close() conn.close()except MySQLdb.Error,e: print 'MySQL Error Msg:',e #建表:单条插入import MySQLdbtry: conn=MySQLdb.connect(host='10.86.10.21',user='root',passwd='mysql',port=3306) cur=conn.cursor() cur.execute('create database if not exists darren') conn.select_db('darren') cur.execute('create table test(id int(10),info varchar(255))') value = ('1','hidarren') print value cur.execute('''insert into test values(%s,%s)''',value) conn.commit() cur.close() conn.close()except MySQLdb.Error,e: print 'MySQL Error Msg:',e#建表,插入多条数据。import MySQLdbtry: conn=MySQLdb.connect(host='10.86.10.21',user='root',passwd='mysql',port=3306) cur=conn.cursor() cur.execute('create database if not exists darren') conn.select_db('darren') cur.execute('create table test(id int(10),info varchar(255))') value = ('1','hidarren') values_list=[] for i in range(20): values_list.append(('1,darren')) cur.executemany ('''insert into test values(%s,%s)''',value) conn.commit() cur.close() conn.close()except MySQLdb.Error,e: print 'MySQL Error Msg:',e ################################################################################################################################################################--------------------------------python socket-------------------------------------------###服务端:import socketHOST=''PORT=50007s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.bind((HOST,PORT))s.listen(1)conn,addr=s.accept()print 'connected by',addrwhile 1: data=conn.recv(1024) if not data:break conn.sendall(data)conn.close()#end###客户端:import sockethost=''port=50007s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect((host,port))s.sendall('hello,world')data=s.recv(1024)s.close()print 'received',repr(data) #end#注意:此处python脚本命名不能为socket.py,如果已经创建提示出错:AttributeError: 'module' object has no attribute 'AF_INET'则rm -rf socket.pyc即可解决。-------------------------------------#客户端另一种情况:import sockethost = '10.86.10.19'port = 50007s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect((host,port))while 1: user_input = raw_input('msg to send::').strip() s.sendall(user_input) data=s.recv(1024) #print 'received:',repr(data) print 'received:',datas.close()#end --------------------------------------服务端:a客户端连接服务端以后如果不断,b不可以连接,b连接以后a不能连接。import sockethost = '0.0.0.0'port = 50007s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.bind((host,port))s.listen(1)while 1: conn,addr=s.accept() while 1: data=conn.recv(1024) if not data:break conn.sendall(data)conn.close()#end###################python socket多线程###################服务端:#!/usr/bin/env pythonimport SocketServerclass mysocketserver(SocketServer.BaseRequestHandler): def handle(self): print 'got a new conn from',self.client_address while True: data = self.request.recv(1024) if not data:break print 'recv:',data self.request.send(data.upper()) #upper为大写if __name__=='__main__':h='0.0.0.0'p=9001s=SocketServer.ThreadingTCPServer((h,p),mysocketserver)s.serve_forever()#end#客户端:import sockethost='10.86.10.17'port=50007s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect((host,port))while True: user_input=raw_input("pelase input you want to:").strip() if len(user_input) == 0:continue s.sendall(user_input) data=s.recv(1024) print 'received:',repr(data) #repr为格式化打印s.close() ##################python socket ssh####################import commands commands.getoutput('ls') #输入命令,返回的是结果。commands.getstatusoutput('ls') #输入命令返回结果加状态import SocketServerclass mysocketserver(SocketServer.BaseRequestHandler): def handle(self): print 'got a new conn from',self.client_address while True: data = self.request.recv(1024) if not data:break print 'recv:',data cmd_result=commands.getstatusoutput(data) self.request.send(len(cmd_result)) self.request.sendall(cmd_result[1])if __name__=='__main__':h='0.0.0.0'p=9001s=SocketServer.ThreadingTCPServer((h,p),mysocketserver)s.serve_forever()