-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
140 lines (125 loc) · 5.31 KB
/
client.py
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
#!/usr/bin/env python
import socket
import argparse
import httplib
DEFAULT_REMOTE_HOST = '127.0.0.1'
DEFAULT_REMOTE_PORT = 8080
class HTTPClient:
def __init__(self, remote_host_name, remote_host_port):
self.host_name = socket.gethostname()
self.ip_address = socket.gethostbyname(self.host_name)
self.remote_host_name = remote_host_name
self.remote_host_port = remote_host_port
def login(self):
userID = raw_input('User ID: ')
password = raw_input('Password: ')
http = httplib.HTTPConnection(self.remote_host_name, self.remote_host_port)
http.putrequest('GET', '/')
http.putheader('Host', self.ip_address)
http.putheader('Authorization', ':'.join([userID, password]))
http.endheaders()
try:
http_response = http.getresponse()
except Exception:
print 'Client failed'
else:
print 'Got message from %s' % self.remote_host_name
print http_response.getheader('Response-content')
if http_response.status == 200: return True
else: return False
def register(self):
userID = raw_input('User ID: ')
password = raw_input('Password: ')
http = httplib.HTTPConnection(self.remote_host_name, self.remote_host_port)
http.putrequest('GET', '/')
http.putheader('Host', self.ip_address)
http.putheader('Register', ':'.join([userID, password]))
http.endheaders()
try:
http_response = http.getresponse()
except Exception:
print 'Client failed'
else:
print 'Got message from %s' % self.remote_host_name
print http_response.getheader('Response-content')
return False
def download(self):
remote_file_path = raw_input('Remote file path: ')
http = httplib.HTTPConnection(self.remote_host_name, self.remote_host_port)
http.putrequest('GET', remote_file_path)
http.putheader('User-Agent', __file__)
http.putheader('Host', self.remote_host_name)
http.putheader('Accept', '*/*')
http.endheaders()
try:
http_response = http.getresponse()
except Exception:
print 'Client failed'
else:
print 'Got file from %s' % self.remote_host_name
print http_response.getheader('Response-content')
remote_file = http_response.read()
dest_file = open(remote_file_path.split('/')[-1], 'w')
dest_file.write(remote_file)
dest_file.close()
def upload(self):
local_file_path = raw_input('Local file path: ')
http = httplib.HTTPConnection(self.remote_host_name, self.remote_host_port)
local_file = open(local_file_path, 'r')
local_file_content = local_file.read()
local_file.close()
http.putrequest('POST', '/' + local_file_path.split('/')[-1])
http.putheader('User-Agent', __file__)
http.putheader('Content-length', len(local_file_content))
http.putheader('Host', self.remote_host_name)
http.putheader('Accept', '*/*')
http.endheaders()
http.send(local_file_content)
try:
http_response = http.getresponse()
except Exception:
print 'Client failed'
else:
print 'Got response from %s' % self.remote_host_name
print http_response.getheader('Response-content')
def main_loop(self):
parser = argparse.ArgumentParser(description='HTTP Client')
parser.add_argument('--file', action='store', dest='filename', required=False)
# given_args = parser.parse_args()
while True:
try:
initTypeString = raw_input('login/register: ')
init_result = {'login':lambda: self.login(), 'register':lambda: self.register()}[initTypeString]()
if not init_result: continue
except KeyError:
print 'Input error...(login, register)'
continue
while True:
try:
requestTypeString = raw_input('Request Type (download, upload): ')
{
'download':lambda: self.download(),
'upload' :lambda: self.upload()
}[requestTypeString]()
except KeyError:
print 'Input error...(download, upload)'
continue
while True:
continueFlagString = raw_input('Next step (continue, exit): ');
try:
{
'continue':lambda x : None,
'exit' :lambda x : exit(x)
}[continueFlagString](1)
except KeyError:
print 'Input error...(continue, exit)'
continue
break
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='HTTP Client')
parser.add_argument('--host', action='store', dest='remote_host', default=DEFAULT_REMOTE_HOST)
parser.add_argument('--port', action='store', dest='remote_port', type=int, default=DEFAULT_REMOTE_PORT)
given_args = parser.parse_args()
host, port = given_args.remote_host, given_args.remote_port
client = HTTPClient(host, port)
client.main_loop()