使用的资料

计算机网络自顶向下方法 第7版

参考前人第六版资料

操作系统:Windows 7

实验步骤

Running the Server Put an HTML file (e.g., HelloWorld.html) in the same directory that the server is in. Run the server program.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>HelloWorld</title>
  6. </head>
  7. <body>
  8. <header>HelloWorld</header>
  9. <p>Socket Programming Assignment 1: Web Server</p>
  10. </body>
  11. </html>

Determine the IP address of the host that is running the server (e.g., 128.238.251.26). From another host, open a browser and provide the corresponding URL. For example:http://128.238.251.26:6789/HelloWorld.html, ‘HelloWorld.html’ is the name of the file you placed in the server directory. Note also the use of the port number after the colon. You need to replace this port number with whatever port you have used in the server code. In the above example, we have used the port number 6789. The browser should then display the contents of HelloWorld.html. If you omit “:6789”, the browser will assume port 80 and you will get the web page from the server only if your server is listening at port 80.Then try to get a file that is not present at the server. You should get a “404 ot Found” message.

这里说明一下:文档中提到用另一台主机访问时的问题,一方面现在IP都是私有地址,不能直接在网络上直接访问,因此我就只能(也只会)采用本机测试。

win7使用cmd,ipconfig查看。

image-20210113163744648.png

代码前半部分根据书上TCPServer.py改写,还需要一些Python读写文件的知识。

# The origin code on the textbook had been out of date, since Python 2.x ended up.
# Try to use Python3.x rewrite this code!

#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
#Fill in start
serverPort = 6789
serverSocket.bind(("", serverPort))
serverSocket.listen(1)
#Fill in end
while True:
    #Establish the connection
    #print 'Ready to serve...'
    print("Ready to serve...") #change the python2.x code
    connectionSocket, addr = serverSocket.accept()
    try:
        message = connectionSocket.recv(1024)
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read()
        #Send one HTTP header line into socket
        #Fill in start
        #top-down approach 7ed P69 HTTP response message
        httpHeader = "HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\n\n"
        connectionSocket.send(httpHeader.encode())
        #Fill in end
        #Send the content of the requested file to the client
        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i].encode())
        connectionSocket.close()
    except IOError:
        #Send response message for file not found
        #Fill in start
        httpHeader = "HTTP/1.1 404 Found"
        connectionSocket.send(httpHeader.encode())
        #Fill in end
        #Close client socket
        #Fill in start
        connectionSocket.close()
        #Fill in end
serverSocket.close()

测试结果

image-20210113162043824.png

image-20210113162120004.png

关于扩展练习

  1. Implement a multithreaded server that is capable of serving multiple requests simultaneously.
  2. Instead of using a browser, write your own HTTP client to test your server.

第一个是让写一个多线程的来响应多个同时的请求;第二个是自己写个HTTP客户端。以个人目前的水平,不太现实。

总结

本次实验是套接字编程的第一个任务,一方面套接字编程之前从来没写过,另一方面Python不是很熟练。