题目:
如何创建套接字,将其绑定到特定的地址和端口,以及发送和接收HTTP数据包。您还将学习一些HTTP首部格式的基础知识。
您将开发一个处理一个HTTP请求的Web服务器。您的Web服务器应该接受并解析HTTP请求,然后从服务器的文件系统获取所请求的文件,创建一个由响应文件组成的HTTP响应消息,前面是首部行,然后将响应直接发送给客户端。如果请求的文件不存在于服务器中,则服务器应该向客户端发送“404 Not Found”差错报文。
这是一个小小的尝试,
其实接下来很多问题都没有解决,比如:
1)多线程怎么发送
2)如果发送的长度大于1024,怎么分段怎么发?
我一开始尝试的是一个字节发一次,然后我的server就Brokenpipe了,所以这肯定也是要解决的问题。
TCP Client:
from socket import *
import sys
if len(sys.argv) < 4:
print("Input your file and port number")
exit(0)
serverName = sys.argv[1]
serverPort = sys.argv[2]
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, int(serverPort)))
sendData = 'GET' + ' /' + sys.argv[3]
clientSocket.send(sendData.encode())
#第一个接收的是header
receiveData = clientSocket.recv(2048).decode()
print(receiveData)
#第二个接收的是html文件
receiveData = clientSocket.recv(2048).decode()
print(receiveData)
clientSocket.close()
TCP Server:
在第26行可以看到,我本来是写的一个字节一个字节的发,因为read是读取文件所有内容在一个字符串里,但是我的receiver是只接受了一次,所以会有broken pipe,可是如何确定为的client要接受多少次呢?就是我一开始说的文件的分组,这是肯定可以解决的,但是我现在还不知道额。第二部分就是并发的socket,希望会有答案吧。
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket, TCP
serverPort = 12000
serverSocket.bind(('',serverPort)); #指定端口必须是一个元组
serverSocket.listen(1) #最大连接数为1
#Bind
while True:
#Establish the connection
print ("Ready to serve...")
connectionSocket, addr = serverSocket.accept()
try:
#For example: GET /page.html HTTP/1.1
#split()[1] = /page.html
#filename[1:] = page.html
message = connectionSocket.recv(1024)
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.read()
#Send one HTTP header line into socket,这是HTTP数据的回应格式
header = 'HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html\nContent-Length: %d\n\n' %(len(outputdata))
connectionSocket.send(header.encode())
#outputdata是一个列表,每一个元素表示每一行,Sent data
#for i in range(0, len(outputdata)):
#print(outputdata[i])
connectionSocket.send(outputdata.encode())
connectionSocket.close()
except IOError:
header = 'HTTP/1.1 404 Not Found'
connectionSocket.send(header.encode())
#Send response message for file not found
connectionSocket.close()
#Close client socket #Fill in start #Fill in end
serverSocket.close()
运行结果:
clients 端:
airdeMacBook-Air-2:计算机网络 air$ python TCPClient.py 172.30.229.14 12000 HelloWorld.html
HTTP/1.1 200 OK
Connection: close
Content-Type: text/html
Content-Length: 915
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1504.83">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000}
span.s1 {font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1">Hello world!</span></p>
<p class="p1"><span class="s1">Hello world!</span></p>
<p class="p1"><span class="s1">Hello world!</span></p>
<p class="p1"><span class="s1">Hello world!</span></p>
<p class="p1"><span class="s1">Hello world!</span></p>
<p class="p1"><span class="s1">Hello world!</span></p>
</body>
</html>
airdeMacBook-Air-2:计算机网络 air$
Server 端:
Last login: Wed May 9 17:09:02 on ttys000
airdeMacBook-Air-2:~ air$ cd ./Desktop/
airdeMacBook-Air-2:Desktop air$ cd ./计算机网络/
airdeMacBook-Air-2:计算机网络 air$ python webserver.py
Ready to serve...
Ready to serve...
Ready to serve...