Python a simple Web server and client

Source: Internet
Author: User

Server:

Create a connection socket when a customer contacts receive an HTTP request (*) from this connection to interpret the specific file requested by the request
Get the file from the server's file system and send the file contents if the file does not exist, then return "404 Not Found" (*)

Client:

Client can establish a TCP connection with the server

Client requests a file on the server side over a TCP connection

Display the contents of the file presented to the client


Note: Before running this file, the server.py directory needs to contain the file folder, which contains the files in the server, the client can request files from the server file.

Readme: First Open the server.py, turn on the server
Then open client.py, enter the file name, including the suffix
The server filesystem is the file folder
Specific information about the file that appears in the client.py command-line window
Press any key to close the client



client.py
[Python]View PlainCopy
  1. Import socket
  2. ServerName = ' 127.0.0.1 '
  3. ServerPort = 50008
  4. Clientsocket = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
  5. Clientsocket.connect ((Servername,serverport))
  6. Print ' Input the HTTP request: '
  7. sentence = ' '
  8. While True:
  9. TMP = Raw_input ()
  10. Sentence = sentence + tmp
  11. if (tmp=="): break
  12. Clientsocket.send (sentence)
  13. Receivesentence = Clientsocket.recv (1024x768)
  14. Print ' from Server: ', receivesentence
  15. Isend = Raw_input ()
  16. Clientsocket.close ()

server.py
[Python]View PlainCopy
  1. Import socket
  2. Import OS
  3. ServerPort = 50008
  4. ServerSocket = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
  5. Serversocket.bind ((' 127.0.0.1 ', serverport))
  6. Serversocket.listen (1)
  7. print ' The server is a ready to receive '
  8. While 1:
  9. Connectionsocket, addr = Serversocket.accept ()
  10. Sentence = Connectionsocket.recv (1024x768)
  11. Ans = "
  12. flag = False;
  13. For ch in sentence:
  14. if (ch = = "and flag = =True) : break
  15. if (flag = = True):
  16. ans = ans + ch;
  17. elif (ch=="):
  18. flag = True;
  19. Path = ' file//' + ans
  20. if (os.path.exists (path) = =False):
  21. Connectionsocket.send (' 404 Not Found ')
  22. Else:
  23. File = open (path,' R ')
  24. while 1:
  25. data = File.read (1024x768)
  26. if not data: break
  27. Connectionsocket.send (data)
  28. File.close ()
  29. Connectionsocket.close ()

Python a simple Web server and client

Related Article

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.