forked from dyf102/CMPUT404-assignment-web-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpclient.py
More file actions
178 lines (141 loc) · 5.06 KB
/
Copy pathhttpclient.py
File metadata and controls
178 lines (141 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# coding: utf-8
# Copyright 2013 Eric Lam, Abram Hindle
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Do not use urllib's HTTP GET and POST mechanisms.
# Write your own HTTP GET and POST
# The point is to understand what you have to send and get experience with it
import sys
import socket
import re
# you may use urllib to encode data appropriately
import urllib
def help():
print "httpclient.py [GET/POST] [URL]\n"
class HTTPRequest(object):
def __init__(self, code=200, body=""):
self.code = code
self.body = body
class HTTPClient(object):
client_socket = socket.socket();
# Get the Host, Port, and Path which will be stored in 'data' in that order
def get_host_port(self,url):
data = []
url = url.split("/")
if ':' in url[2]:
#print url
data.append(url[2].split(":")[0])
data.append(int(url[2].split(":")[1]))
path = ""
for x in range(3, len(url)):
path += "/" + url[x]
data.append(path)
else:
#print url
data.append(url[2])
data.append(80)
path = ""
for x in range(3, len(url)):
path += "/" + url[x]
data.append(path)
#print "DATA IS HERE"
#print data
return data
# Connect to the server
def connect(self, host, port):
# use sockets!
#create an INET, STREAMing socket
try:
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print 'Failed to create socket'
sys.exit()
print 'Socket Created'
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
#could not resolve
print 'Hostname could not be resolved. Exiting'
sys.exit()
#Connect to remote server
self.client_socket.connect((remote_ip , port))
print 'Socket Connected to ' + host + ' on ip ' + remote_ip
return None
# Get the HTTP Status Code
def get_code(self, data):
data = data.split(' ')
return int(data[1])
# read everything from the socket
def recvall(self, sock):
buffer = bytearray()
done = False
while not done:
part = sock.recv(1024)
if (part):
buffer.extend(part)
else:
done = not part
return str(buffer)
def GET(self, url, args=None):
# 0 is the Host, 1 is the Port, and 2 is the Path
data = self.get_host_port(url)
self.connect(data[0], data[1])
if(args != None):
args = urllib.urlencode(args)
data[2] = data[2] + "?" + args
request = "GET " + data[2] + " HTTP/1.1\r\nHost: "+data[0]+":"+str(data[1])+"\r\nConnection: keep-alive\r\nContent-Type: text/html\r\n"
request += "\r\n"
#print "GET REQUEST IS HERE"
#print request
self.client_socket.send(request)
body = self.recvall(self.client_socket)
#print "GET BODY IS HERE"
#print body
code = self.get_code(body)
return HTTPRequest(code, body)
def POST(self, url, args=None):
data = self.get_host_port(url)
self.connect(data[0], data[1])
request = "POST " + data[2] + " HTTP/1.1\r\nHost:"+data[0]+":"+str(data[1])+"\r\nConnection: keep-alive\r\nContent-Type: application/x-www-form-urlencoded\r\n"
if(args != None):
args = urllib.urlencode(args)
request += 'Content-Length: %s \r\n' % (len(args))
request += "\r\n"
request += args + "\r\n"
request += "\r\n"
#print "REQUEST IS HERE"
#print request
self.client_socket.send(request)
body = self.recvall(self.client_socket)
#print "BODY IS HERE"
#print body
code = self.get_code(body)
body = body.splitlines()
body = body[len(body)-1]
return HTTPRequest(code, body)
def command(self, url, command="GET", args=None):
if (command == "POST"):
return self.POST( url, args )
else:
return self.GET( url, args )
if __name__ == "__main__":
client = HTTPClient()
command = "GET"
if (len(sys.argv) <= 1):
help()
sys.exit(1)
elif (len(sys.argv) == 3):
print client.command( sys.argv[1], sys.argv[2] )
else:
print client.command( command, sys.argv[1] )