联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2019-04-13 11:50

Homework 4

Introduction to Networks and Their Applications

Due: April 11th, 2019

You are to develop a simple web server in Python. The web server must be started

with a two arguments specifying the IP address of the interface on which the server will

bind to and the port number on which the server will run. The server should handle http

requests such as (assuming the server is running on port 8000):

http://127.0.0.1:8000/index.html

http://127.0.0.1:8000/cars/ford.html

The server will store html static content in a local directory called static. Accordingly,

the above URLs will be resolved to the following files on the server:

URL file location

http://127.0.0.1:8000/index.html static/index.html

http://127.0.0.1:8000/cars/ford.html static/cars/ford.html

The server should implement the following logic:

Connection Setup: Create a socket to wait for incoming connections. The server

should be able to listen to 5 pending connections. Upon receiving a connection, you

should accept it and process the incoming http request as discussed in the next step.

Multiple Connections: You should support multiple connections and multiplex

between them using select.

HTTP Request: Your server should only handle HTTP GET requests. A complete

description of how an http request is formatted may be found in RFC 2616. For the

purposes of this assignment it is safe to assume the following restricted format:

GET SP Request-URI SP HTTP/1.1 CRLF

(HTTPKey:HTTPValue CRLF)*

CRLF

1

In the above, the following notation is used: the text in blue will appear as it is in

the header packet. SP stands for space and CRLF for ’\r\n’. The RequestURI is the

request URI. The HTTPKey:HTTPValue are header fields that may appear. The

()* indicates that there could be zero or more header fields. Your server should read

these fields and make sure the header is formatted correct but it can safely ignore

them (i.e., you do not need to take any actions based on their values). The server

must make sure that the incoming request is consistent with the above format. It

is safe to assume that the RequestURI will not exceed 255 bytes. This functionality

should be process http header(socket) that returns the URI that is requested in

the case when the request is correctly formatted or None otherwise.

Responding to an HTTP request: There are three possible outcomes when

handling an HTTP request:

1. Bad Request: the HTTP request was invalid (i.e., it does not copy with the

above format). In this case your server should respond with a Bad Request

reply.

2. Not Found: if the HTTP request is valid, then your program the process http header

should store the RequestURI into the memory location pointed by uri variable.

The RequestURI must be translated into a file location as indicated in the URL

to file mapping table. After performing this translation, the server must check

if the request file exists. If it does not, the server should respond with a Not

Found error.

3. No errors: in the case when the file exists, the server shall read the contents of

the file and generate an HTTP reply.

Generating an HTTP reply: In the case of finding the appropriate file to server

on the web server, you need to send the content. However, you need to generate an

appropriate HTTP header. In our case the minimal HTTP header is:

HTTP/1.1 SP 200 SP OK CRLF

Content-Type: text/html

CRLF

CRLF

After sending the header, you can send the contents of the file.

As usual, you have to submit your homework through ICON. The following resources

may be useful in solving the homework: HTTP RFC (https://tools.ietf.org/html/

rfc2616).

2


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp