首页 > 代码库 > 第十一节:python mysql交互、socket、多线程
第十一节:python mysql交互、socket、多线程
python个人笔记,纯属方便查询。
------------------------------------python mysql交互--------------------------------------- #查询: import MySQLdb try: 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 MySQLdb try: 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 MySQLdb try: 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 socket HOST=‘‘ PORT=50007 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((HOST,PORT)) s.listen(1) conn,addr=s.accept() print ‘connected by‘,addr while 1: data=conn.recv(1024) if not data:break conn.sendall(data) conn.close() #end ###客户端: import socket host=‘‘ port=50007 s=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 socket host = ‘10.86.10.19‘ port = 50007 s = 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:‘,data s.close() #end -------------------------------------- 服务端:a客户端连接服务端以后如果不断,b不可以连接,b连接以后a不能连接。 import socket host = ‘0.0.0.0‘ port = 50007 s = 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 python import SocketServer class 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=9001 s=SocketServer.ThreadingTCPServer((h,p),mysocketserver) s.serve_forever() #end #客户端: import socket host=‘10.86.10.17‘ port=50007 s=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 SocketServer class 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=9001 s=SocketServer.ThreadingTCPServer((h,p),mysocketserver) s.serve_forever()
本文出自 “小东哥” 博客,谢绝转载!
第十一节:python mysql交互、socket、多线程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。