Simulation of an online music player (socket + database)

Simulation of an online music player (database + socket (TCP protocol))

1, using the C / S architecture design, respectively, to write the client and server programs,

2, after the client linked server, the server prompts the song list to the user can choose to start playing (audio files can be stored locally) after the user selects. You do not need to pause, the song and other functions

3, the package needs to function as a common tool module (.py files), and subjected to call

 

1. Create a database data

. 1  . 1 . Creating Database
 2     Create  Database Song;
 . 3     use Song;
 . 4  
. 5  2 . Create a table
 . 6     Create  Table t_list (
 . 7     ID int  Primary  Key AUTO_INCREMENT,
 . 8     name VARCHAR ( 32 ) Not  null ,
 . 9     Link VARCHAR ( 2000 ) Not  null 
10     ); 
. 11 . 3 . calling database data is inserted (the latter field need to put the local link path music files on the computer, for later playback file) 12 Insert into t_list(name,link) values('stay_with_me', '/Users/.../StayWithMe.mp3'); 13 insert into t_list(name,link) values('Virtual Riot Lift Me_Up', '/Users/.../VirtualRiotLiftMe_Up.mp3'); 14 insert into t_list(name,link) values('阿刁', '/Users/.../阿刁.mp3');

 

2. In the function .py files in the database to do some simple package (db_helper.py)

. 1  Import pymysql
 2  
. 3  
. 4  DEF get_conn (): # connect to the database to make the individually packaged
 . 5      CON = pymysql.connect (
 . 6          Host = ' localhost ' ,
 . 7          User = ' the root ' ,
 . 8          password = '123123 ' ,
 . 9          DB = ' Song ' , database name equal to the desired value of # db query
 10          charset = ' UTF8 ' 
. 11      )
 12 is      return CON
 13 is 
14  
15  DEF search_s (SQL): # queries the database to make the package function
 16      CON = get_conn ()
 . 17      Cursor = con.cursor ()
 18 is      the cursor.execute (SQL)
 . 19      Data = cursor.fetchall ()
 20 is      return Data
 21 is  
22 is  
23 is  DEF instert_s (SQL): # inserting data into a database to make the package (present in less than)
 24      CON = get_conn ()
 25      Cursor = con.cursor ()
 26 is      the cursor.execute (SQL)
 27      con.commit ()

 

3. Write socket server

  The main function: with socket client communication, sent to the client database list of songs, song number selected by the user, the server queries from the database to the local path corresponding to the song to the client

. 1  from Socket Import *
 2  from db_helper Import * # db_helper .py file name is above the package database functions
 . 3  Import struct
 . 4  
. 5  
. 6 Server = General Settings socket () # socket server
 . 7 server.setsockopt (SOL_SOCKET, the SO_REUSEADDR,. 1 )
 . 8 server.bind (( "" , 8081 ))
 . 9 server.listen (. 5 )
 10  
. 11  Print ( ' server-initiated ... ' )
 12 is  
13 is  the while True:
 14      LST =[]
 15      Total = '' 
16  
. 17      newsocket, addr = server.accept ()
 18 is      recv_data = newsocket.recv (1024 )
 . 19  
20 is      DB_DATA = search_s ( ' SELECT ID, name from t_list ' ) # call query method in db_helper, obtaining from the database id and the song
 21 is  
22 is      for I in DB_DATA:
 23 is          new new STR = (I [0]) + '  ' + I [. 1 ]
 24          lst.append (new new)
 25  
26 is      Total = ' & ' .join (LST)
 27     = The name_len len (Total)
 28  
29      length struct.pack = ( ' I ' , The name_len) by the length of the string # struct package to the client (server socket solve problems dip package)
 30      newsocket.send (length)
 31 is      newsocket .send (total.encode ())
 32  
33 is      the select1 = newsocket.recv (1024 )
 34 is      S = select1.decode ()
 35      db_data1 search_s = (F ' SELECT t_list Link from {S} WHERE ID = ' ) acquired user # after selecting a song id, to query the database of songs corresponding to the link
 36  
37 [      newsocket.send (db_data1 [0] [0] .encode ()) # transmits to the client link

 

4. socket client

  Start playing local music files with the communications socket server, receives data sent by the server and print, send the user to select the song number to the server, access to the link: the main function

 1 import socket
 2 import subprocess
 3 import struct
 4 
 5 lst = []
 6 
 7 client = socket.socket()    # socket客户端常规设置
 8 client.connect(('127.0.0.1', 8081))
 9 
10 while True:
11     client.send('song_list'.encode())
12     recv_num = client.recv(4)
13     total_size = struct.unpack('i', recv_num)[0]
14 
15     recv_size = 0
16     recv_msg = b''
17 
18     while recv_size < total_size:
19 
20         every_recv = client.recv(1024)
21         recv_msg += every_recv
22         recv_size += len(every_recv)
23 
24     data1 = recv_msg.decode()
25 
26     lst1 = data1.split('&')
27 
28     for i in lst1:
29         print(i) # received from the server to print the song list
 30      BREAK 
31 is  
32  the try :
 33 is      the Choose = INPUT ( ' Enter ID: ' ) .strip ()
 34 is  
35      client.send (choose.encode ())
 36      filename = Client .recv (1024 ) .decode () Gets the song # local path sent from the server
 37 [  
38 is      Print ( ' song playback ... ' )
 39      the return_code subprocess.call = ([ " afplay " , filename])
 40  
41 is      client.close ()
 42 is  
43 is  the except Exception:
 44 is     pass

Guess you like

Origin www.cnblogs.com/wuaihua/p/12200041.html