11th: Python mysql interaction, socket, multithreading

Source: Internet
Author: User

Python personal notes are purely convenient to query.

------------------------------------Python mysql Interactive---------------------------------------  #查询: 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          #建表: Single Insert 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), 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# build the table, insert more than one data. 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 (Ten), Info varchar (255)) ')          value =  (' 1 ', ' Hidarren ')          values_list=[]        for i in range ( ):         values_list.append ((' 1,darren '))          cur.executemany  ("Insert into test values (%s,%s)", value) &NBsp;       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 (1024x768)     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 (1024x768) s.close () print  ' received ', repr (data)   #end # Note: The python script name here cannot be socket.py, if you have created a prompt error:attributeerror:  ' module '  object has no  attribute  ' af_inet ' can be solved by 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 (1024x768)           #print   ' Received: ', repr (data)           print  ' Received: ', Datas.close () #end  --------------------------------------Server: A client connects to the server and, if ever, B can not connect, B connection after a cannot connect. 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 (1024x768)                  if not data:break                 conn.sendall (data) Conn.close () #end ################## #python  socket multithreading ################## #服务端:#!/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 # client: 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 (1024x768)          print  ' Received: ', repr (data)                #repr为格式化打印s. Close ()  ################# #python  socket ssh################### #import  commands commands.getoutput (' ls ')           #输入命令, the result is returned. 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 ( 1024x768)  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 ()


This article from "Small East elder brother" blog, declined reprint!

11th: Python mysql interaction, socket, multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.