Example #1
0
 def replace_chain():
     network=self.nodes
     longest_chain=None
     max_lenght=len(self.chain)
     for node in network:
         response=requests.get(f'http://{node}/get_chain')
         if response.status_code == 200:
             length=json.response()['lenght']
             chain=json.response()['chain']
             if lenght > max_lenght and self.is_valid_chain(chain):
                 max_length=length
                 longest_chain=chain
     if longest_chain:
         self.chain=longest_chain
         return True
     return False
Example #2
0
def authorize(url, method, token):
    # Extract user information from url. Determine whether the methond should be approved.
    u = urlparse(url)
    action = u[2].split("/")

    approve_anyuser = ["GET", "PUT"]
    approve_admin = ["GET", "PUT", "DELETE"]
    approve_onlyposter = ["POST"]
    if token:
        try:
            info = jwt.decode(token, key="jwt-secret")
        except (jwt.DecodeError, jwt.ExpiredSignatureError):
            return json.response({'message': 'Token is not valid'})
    email = ""
    uid = ""
    for i in action:
        if "@" in i:
            email = i
            break
        if "-" in i:
            uid = i
            break

    if method in approve_onlyposter:
        if email == token['email'] or uid == token["uid"]:
            info['operations'] = approve_onlyposter
        else:
            raise (ActionException(ActionException.unproved_action))
    elif method in approve_admin:
        if info['role'] != 'admin':
            raise (ActionException(ActionException.unproved_action))
        else:
            info['operations'] = approve_admin
    else:
        info["operations"] = approve_anyuser

    h = jwt.encode(info, key=_context.get_context("jwt-secret"))
    h = str(h)

    return h
Example #3
0
#Write a program myip.py to print the external IP address of the machine. Use the response from http://httpbin.org/get

import urllib
import json
print 'loading............'
print json.response(urllib.urlopen('http://httpbin.org/get'))['origin']
Example #4
0
# Write a program myip.py to print the external IP address of the machine. Use the response from http://httpbin.org/get

import urllib
import json

print "loading............"
print json.response(urllib.urlopen("http://httpbin.org/get"))["origin"]
Example #5
0
 def POST(self):
     data = json.request()
     db.todos.save(data, safe=True)
     raise json.response(web.created, data=data)
Example #6
0
 def GET(self):
     raise json.response(web.ok, data=list(db.todos.find()))