-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclient.py
More file actions
executable file
·174 lines (144 loc) · 7.5 KB
/
client.py
File metadata and controls
executable file
·174 lines (144 loc) · 7.5 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
#.!/usr/bin/env python3
import sys, json, os, argparse
import requests
# import from the 21 Developer Library
#from two1.commands.config import Config
#from two1.lib.wallet import Wallet
#from two1.lib.bitrequests import BitTransferRequests
# set up bitrequest client for BitTransfer requests
#wallet = Wallet()
#username = Config().username
#requests = BitTransferRequests(wallet, username)
# server address
def request(args):
primary_address = args.address
sel_url = "{0}request?address={1}&contact={2}"
answer = requests.get(url=sel_url.format(args.url, primary_address, args.contact))
if answer.status_code != 200:
print("Request failed.")
else:
print(answer.text)
def buy(args):
primary_address = wallet.get_payout_address()
sel_url = "{0}buy?address={1}&contact={2}"
answer = requests.get(url=sel_url.format(args.url, primary_address, args.contact))
if answer.status_code != 200:
print("Could not make offchain payment. Please check that you have sufficient balance.")
else:
print(answer.text)
def put(args):
primary_address = wallet.get_payout_address()
message = args.key + args.value + primary_address + args.nonce
signature = wallet.sign_message(message)
data = {"key": args.key,
"value": args.value,
"nonce": args.nonce,
"signature": signature,
"address": primary_address}
sel_url = "{0}put"
body = json.dumps(data)
headers = {'Content-Type': 'application/json'}
answer = requests.post(url=sel_url.format(args.url), headers=headers, data=body)
print(answer.text)
def delete(args):
primary_address = wallet.get_payout_address()
message = args.key + primary_address + args.nonce
signature = wallet.sign_message(message)
data = {"key": args.key,
"nonce": args.nonce,
"signature": signature,
"address": primary_address}
sel_url = "{0}delete"
body = json.dumps(data)
headers = {'Content-Type': 'application/json'}
answer = requests.post(url=sel_url.format(args.url), headers=headers, data=body)
print(answer.text)
def get(args):
sel_url = "{0}get?key={1}"
answer = requests.get(url=sel_url.format(args.url, args.key))
print(answer.text)
def buy_file(server_url = 'http://localhost:5000/'):
# get the file listing from the server
response = requests.get(url=server_url+'files')
file_list = json.loads(response.text)
# print the file list to the console
for file in range(len(file_list)):
print("{}. {}\t{}".format(file+1, file_list[str(file+1)][0], file_list[str(file+1)][1]))
try:
# prompt the user to input the index number of the file to be purchased
sel = input("Please enter the index of the file that you would like to purchase:")
# check if the input index is valid key in file_list dict
if sel in file_list:
print('You selected {} in our database'.format(file_list[sel][0]))
#create a 402 request with the server payout address
sel_url = server_url+'buy?selection={0}&payout_address={1}'
answer = requests.get(url=sel_url.format(int(sel), wallet.get_payout_address()), stream=True)
if answer.status_code != 200:
print("Could not make an offchain payment. Please check that you have sufficient balance.")
else:
# open a file with the same name as the file being purchased and stream the data into it.
filename = file_list[str(sel)][0]
with open(filename,'wb') as fd:
for chunk in answer.iter_content(4096):
fd.write(chunk)
fd.close()
print('Congratulations, you just purchased a file for bitcoin!')
else:
print("That is an invalid selection.")
except ValueError:
print("That is an invalid input. Only numerical inputs are accepted.")
def nonce(args):
primary_address = wallet.get_payout_address()
sel_url = args.url + 'nonce?address={0}'
answer = requests.get(url=sel_url.format(primary_address))
print(answer.text)
def address(args):
primary_address = wallet.get_payout_address()
sel_url = args.url + 'address?contact={0}&address={1}&signature={2}'
answer = requests.get(url=sel_url.format(args.contact, primary_address, args.signature))
print(answer.text)
def help(args):
print("Please run with --help")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Interact with Causeway server")
#parser.set_defaults(func=help)
subparsers = parser.add_subparsers(help="Commands")
parser_buy = subparsers.add_parser('request', help="Request free hosting bucket")
parser_buy.add_argument('url', help='Url of the Causeway server with trailing slash.')
parser_buy.add_argument('address', help='Address used as username for the service.')
parser_buy.add_argument('contact', help='Email address to contact on expiration.')
parser_buy.set_defaults(func=request)
parser_buy = subparsers.add_parser('buy', help="Purchase hosting bucket")
parser_buy.add_argument('url', help='Url of the Causeway server with trailing slash.')
#parser_buy.add_argument('address', help='Address used as username for the service.')
parser_buy.add_argument('contact', help='Email address to contact on expiration.')
parser_buy.set_defaults(func=buy)
parser_put = subparsers.add_parser('put', help="Set or update a value for a key")
parser_put.add_argument('url', help='Url of the Causeway server with trailing slash.')
#parser_put.add_argument('address', help='Address used as username for the service.')
parser_put.add_argument('key', help='Data storage key')
parser_put.add_argument('value', help='Data stored by key')
parser_put.add_argument('nonce', help='Nonce for signature uniqueness.')
parser_put.set_defaults(func=put)
parser_delete = subparsers.add_parser('delete', help="Delete a key/value pair.")
parser_delete.add_argument('url', help='Url of the Causeway server with trailing slash.')
#parser_delete.add_argument('address', help='Address used as username for the service.')
parser_delete.add_argument('key', help='Data storage key')
parser_delete.add_argument('nonce', help='Nonce for signature uniqueness.')
parser_delete.set_defaults(func=delete)
parser_get = subparsers.add_parser('get', help="Download the value stored with a key")
parser_get.add_argument('url', help='Url of the Causeway server with trailing slash.')
parser_get.add_argument('key', help='Key to retrieve')
parser_get.set_defaults(func=get)
parser_nonce = subparsers.add_parser('nonce', help="Get nonce for the address")
parser_nonce.add_argument('url', help='Url of the Causeway server with trailing slash.')
#parser_nonce.add_argument('address', help='Address used as username for the service.')
parser_nonce.set_defaults(func=nonce)
parser_address = subparsers.add_parser('address', help="Get a deposit address")
parser_address.add_argument('url', help='Url of the Causeway server with trailing slash.')
parser_address.add_argument('contact', help='Email address to contact on expiration.')
parser_address.add_argument('address', help='Address used as username for the service.')
parser_address.add_argument('signature', help='Signature of "contact,address" using address\' privkey')
parser_address.set_defaults(func=address)
args = parser.parse_args()
args.func(args)