Python Simple server program

Source: Internet
Author: User

This is an exercise in the Python Core programming (second edition of the article), which asks the server to recognize the following commands:

LS returns the current directory of the server program

OS returns information about the server's operating system

Date gets the current time of the server

LS dir returns the directory dir file list

Server program:

1 Import Time2 ImportOS3 Importdatetime4 5host="'6port=212347Addr=(Host,port)8bufsize=80969 Tensersock=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) OneSersock.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1) A Sersock.bind (ADDR) -Sersock.listen (5) -  the defprocess_cmd (cmd): -     Print "The command receive from client is:%s"%cmd -     ifcmd=='ls': -Curdir=Os.curdir +res=Os.listdir (CurDir) -     elifcmd=='OS': +res=Os.uname () A     elifCmd.startswith ('ls'): atLength=Len (Cmd.split ()) -         ifLength==1: -res="command is illegal" -         eliflength==2: -Reqdir=cmd.split () [1] -             Try: inres=Os.listdir (Reqdir) -             exceptoserror,e: tores=e +         Else: -res="argument is illegal" the     elifcmd=='Date': *res=Datetime.date.today () $     Else:Panax Notoginsengres='command is illegal' -     PrintRes the     returnStr (RES) +  whileTrue: AClisock,addr=sersock.accept () the     Print "... connected from%s:%s"%Addr +      whileTrue: -Cmd=clisock.recv (BUFSIZE) $         if  notcmd: $              Break -         Else: -res=process_cmd (cmd) the clisock.send (RES) - clisock.close ()WuyiSersock.close ()

Client program:

1 #!/usr/bin/python2 3 ImportSocket4 5host='localhost'6port=212347Addr=(Host,port)8bufsiz=80969 Tenclisock=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) One Clisock.connect (ADDR) A Print "Please input below command." - Print "*************************************" - Print "date ls/ls dir os" the Print "*************************************" -  whileTrue: -Cmd=raw_input (">") -     if  notcmd: +         Continue - clisock.send (cmd) +res=clisock.recv (Bufsiz) A     if  notRes: at         Print "response from server error." -     PrintRes -  -Clisock.close ()

Execution Result:

Please input below command.*************************************Date   ls/ls dirOS>Date -- .- the>ls['client_select.py','client.py','server_select.py','chat_server_v1.py','server.py','daytimeclient.py','chat_client_v1.py','chat_server_v2.py','tstserv.py','tstclnt.py']>ls/home['Tmyyss']>OS ('Linux','Ubuntu','3.13.0-53-generic','#89-ubuntu SMP Wed may 10:34:28 UTC','i686')>>>>>Exitcommand is illegal>>>Qcommand is illegal

Python Simple server program

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.