import os
import os.path
import sys
import importlib
if os.path.isdir(os.path.join("../../..", "modules")):
module_dir = os.path.join("../../..", "modules")
else:
module_dir = os.path.join("../..", "modules")
module_path = os.path.abspath(module_dir)
if not module_path in sys.path:
sys.path.append(module_path)
import mysocket as sock
importlib.reload(sock)
connection
. request_message
. This will use a an HTTP method (GET
) and will include a header Host
with value $\textit{host}$ and use \textit{resource path} as the URI in the request-line. send
of the string request_message
over connection
. receive
of the HTTP message response from connection
. Assuming a valid response message, this must retrieve the string of characters up to and including the \textit{empty-line} after the message headers, and then, based on additional information, must retrieve the body
of the response. close()
operation on connection
. Function | Description |
---|---|
makeConnection(host, port) |
Establish a TCP connection from the client machine to a server at the given machine host and listening at the given port . This returns the socket connection. This corresponds to Step 1 of the client-side steps. |
sendString(conn, s) |
Given an established socket conn , take s , a string, and send it over the connection. This corresponds to Step 3 of the client-side steps, where s would define all the characters making up a complete HTTP request. |
receiveTillClose(conn) |
This performs a socket recv() from the connection, consuming data until the server closes the connection. This returns the complete HTTP response message. This corresponds to Step 4 of the client-side steps, and assumes that a connection close will define the end of the response message. |
Step 1
connection = sock.makeConnection("httpbin.org", 80)
assert connection is not None
Step 2
request_line = 'GET / HTTP/1.1\r\n'
host_line = 'Host: httpbin.org\r\n'
one_and_done = 'Connection: close\r\n'
empty_line = '\r\n'
request_message = request_line + host_line + \
one_and_done + empty_line
Step 3
sock.sendString(connection, request_message)
Step 4
response = sock.receiveTillClose(connection)
Step 5
connection.close()
Showing the first 250 characters in the response ...
print(repr(response[:250]))
And printing the first 250 characters of the respose ...
print(response[:250])
Q Now repeat the process for the resource /basic.html
at the server datasystems.denison.edu
, still using port 80. Name the connection c2
, the full message to send request
, and the resoonse r
.
# YOUR SOLUTION HERE
url = "http://datasystems.denison.edu/basic.html"
s = !curl -v -s $url
s
help(sock.makeConnection)