예제 #1
0
    def __init__(self, duel_update_timeout=DUEL_UPDATE_TIMEOUT):
        self.duel_update_timeout = duel_update_timeout

        self.users = set()
        self.duels = []

        self.duel_update_thread = threading.Thread(target=self.update_duels)
        self.dmoj_request_manager = RequestManager(request_submissions)
예제 #2
0
 def __init__(self):
     AppWrapper.__init__(self)
     AppClient.__init__(self, wrapper=self)
     RequestManager.__init__(self)
     self.ledger = Ledger()
     try:
         signal.signal(signal.SIGINT, self.interruptHandler)
         signal.signal(signal.SIGTSTP, self.statusHandler)
     except AttributeError:
         console().warning("Warning. Unable to Bind a Signal Handler.")
예제 #3
0
class ImageUploaderService:
    
    def __init__(self, url):
        self.url = url
        self.request_manager = RequestManager()

    def send_image(self, image, path):
        self.request_manager.post(self.url, image, path, 1)
        
    def get_image(self, path):
        with open(path, "rb") as img_file:
            return base64.b64encode(img_file.read()).decode('utf-8')
    def apply_borrow_money(self, symbol, currency, amount):
        method = "POST"
        path = "/v1/margin/orders"
        url = self.BASE_URL + path

        params = {
            'SignatureMethod': 'HmacSHA256',
            'SignatureVersion': '2',
            'AccessKeyId': self.Access_Key,
            'Timestamp': self.utc_now(),
        }

        params["Signature"] = self.generate_signature(method, params, url)
        params = urllib.parse.urlencode(params)


        data = {
            'symbol': symbol,
            'currency': currency,
            'amount': amount,
        }

        data = json.dumps(data, separators=(',', ':'))

        my_request = Request(
            method=method,
            url=url,
            data=data,
            params=params
        )
        return RequestManager().send_request(my_request, self.is_proxies)
    def create_order(self,symbol,type,amount,price=None,stopprice=None,operator=None):
        account_id = self.spot_account_id
        method = 'POST'
        path = '/v1/order/orders/place'
        url = self.BASE_URL + path

        params = {
            'SignatureMethod': 'HmacSHA256',
            'SignatureVersion': '2',
            'AccessKeyId': self.Access_Key,
            'Timestamp': self.utc_now(),
        }
        params["Signature"] = self.generate_signature(method, params, url)
        params = urllib.parse.urlencode(params)

        data = {}
        data['account-id'] = account_id
        data['symbol'] = symbol
        data['type'] = type
        data['amount'] = amount
        data['price'] = price
        data['stop-price'] = stopprice
        data['operator'] = operator

        data = json.dumps(data, separators=(',', ':'))


        my_request = Request(
            method=method,
            url=url,
            data=data,
            params=params
        )
        return RequestManager().send_request(my_request, self.is_proxies)
예제 #6
0
    def get_swap_financial_record(self,margin_account):
        method = "POST"
        path = "linear-swap-api/v1/swap_financial_record"
        url = self.BASE_URL + path
        params = {
            'SignatureMethod': 'HmacSHA256',
            'SignatureVersion': '2',
            'AccessKeyId': self.Access_Key,
            'Timestamp': self.utc_now(),
        }
        params["Signature"] = self.generate_signature(method, params, url)
        params = urllib.parse.urlencode(params)

        data = {
            'margin_account': margin_account
        }
        data = json.dumps(data, separators=(',', ':'))

        my_request = Request(
            method=method,
            url=url,
            data=data,
            params=params
        )
        return RequestManager().send_request(my_request, self.is_proxies)
예제 #7
0
def test_timeout():
    """Checks if connection will timeout after 5 seconds
  """
    with RequestManager() as ch:
        # connect and just sleep for 6s
        time.sleep(6)
        assert ch.is_socket_closed() == True
예제 #8
0
    def get_swap_position_info(self,contract_code=None):
        method = "POST"
        path = "linear-swap-api/v1/swap_position_info"
        url = self.BASE_URL + path
        params = {
            'SignatureMethod': 'HmacSHA256',
            'SignatureVersion': '2',
            'AccessKeyId': self.Access_Key,
            'Timestamp': self.utc_now(),
        }
        params["Signature"] = self.generate_signature(method, params, url)
        params = urllib.parse.urlencode(params)

        data = {
            'contract_code': contract_code
        }
        data = json.dumps(data, separators=(',', ':'))

        my_request = Request(
            method=method,
            url=url,
            data=data,
            params=params
        )
        return RequestManager().send_request(my_request, self.is_proxies)
예제 #9
0
    def cancel_tpsl_order_all(self, contract_code, direction = None):
        method = 'POST'
        path = 'linear-swap-api/v1/swap_tpsl_cancelall'
        url = self.BASE_URL + path

        params = {
            'SignatureMethod': 'HmacSHA256',
            'SignatureVersion': '2',
            'AccessKeyId': self.Access_Key,
            'Timestamp': self.utc_now(),
        }
        params["Signature"] = self.generate_signature(method, params, url)
        params = urllib.parse.urlencode(params)

        data = {}
        data['contract_code'] = contract_code
        data['direction'] = direction

        data = json.dumps(data, separators=(',', ':'))

        my_request = Request(
            method=method,
            url=url,
            data=data,
            params=params
        )
        return RequestManager().send_request(my_request, self.is_proxies)
예제 #10
0
    def create_order(self,contract_code,volume,direction,offset,lever_rate,order_price_type,price=None):
        method = 'POST'
        path = 'linear-swap-api/v1/swap_order'
        url = self.BASE_URL + path

        params = {
            'SignatureMethod': 'HmacSHA256',
            'SignatureVersion': '2',
            'AccessKeyId': self.Access_Key,
            'Timestamp': self.utc_now(),
        }
        params["Signature"] = self.generate_signature(method, params, url)
        params = urllib.parse.urlencode(params)

        data = {}
        data['contract_code'] = contract_code
        data['volume'] = volume
        data['direction'] = direction
        data['offset'] = offset
        data['lever_rate'] = lever_rate
        data['order_price_type'] = order_price_type
        data['price'] = price

        data = json.dumps(data, separators=(',', ':'))

        my_request = Request(
            method=method,
            url=url,
            data=data,
            params=params
        )
        return RequestManager().send_request(my_request, self.is_proxies)
    def get_history_orders(self,symbol,state,start_time=None):
        method = 'GET'
        path = '/v1/order/orders'
        url = self.BASE_URL + path
        params = {
            'SignatureMethod': 'HmacSHA256',
            'SignatureVersion': '2',
            'AccessKeyId': self.Access_Key,
            'Timestamp': self.utc_now(),
        }
        params['symbol'] = symbol
        params['states'] = state
        if start_time != None:
            params['start-time'] = start_time
        # params['start-time'] = start_time
        # params['end-time'] = end_time

        params["Signature"] = self.generate_signature(method, params, url)
        params = urllib.parse.urlencode(params)

        my_request = Request(
            method=method,
            url=self.BASE_URL + path,
            params=params
        )
        return RequestManager().send_request(my_request, self.is_proxies)
예제 #12
0
def test_host_missing():
    """Checks if sending a request without Host header returns 400
  """
    with RequestManager() as ch:
        ch.send(b"GET /index.html HTTP/1.1\r\nFoo: Bar\r\n\r\n")
        res = ch.read_get()
        assert ch.is_socket_closed() == True
    assert res["status_code"] == 400
예제 #13
0
def test_404():
    """Checks if non-existant file will return 404
  """
    with RequestManager() as ch:
        r = b"GET /foo-bar.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
        ch.send(r)
        res = ch.read_get()
    assert res["status_code"] == 404
예제 #14
0
def test_root_escape():
    """Checks if root escape attempt will return 404
  """
    with RequestManager() as ch:
        r = b"GET /../src/main/main.go HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
        ch.send(r)
        res = ch.read_get()
    assert res["status_code"] == 404
예제 #15
0
def test_chunked_request():
    """Checks if sending the request part by part would work
  """
    with RequestManager() as ch:
        ch.send(b"GET /index.html HTTP/1.1\r\n")
        time.sleep(1)
        ch.send(b"Host: 127.0.0.1\r\n\r\n")
        res = ch.read_get()
    assert res["body"] == root_index
예제 #16
0
def test_default_mime():
    """Checks if the mime-type is "application/octet-stream" for unknown filetype
  """
    with RequestManager() as ch:
        r = b"GET /subdir1/subdir11/maoyo.giaogiao HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
        ch.send(r)
        res = ch.read_get()
    assert res["status_code"] == 200
    assert res["headers"]["Content-Type"] == "application/octet-stream"
예제 #17
0
 def thread_fn(id):
     with RequestManager() as ch:
         ch.send(b"GET /index.html HTTP/1.1\r\n")
         print("Request #%d is sleeping" % id)
         time.sleep(2)
         ch.send(b"Host: 127.0.0.1\r\n\r\n")
         res = ch.read_get()
     print("Request #%d is complete" % id)
     assert res["body"] == root_index
예제 #18
0
def test_partial():
    """Checks if partial http requests result in 400 + close (after 5 sec)
  """
    with RequestManager() as ch:
        ch.send(b"GET /index.html HTTP/1.1\r\n")
        time.sleep(6)
        res = ch.read_get()
        assert ch.is_socket_closed() == True
    assert res["status_code"] == 400
예제 #19
0
def test_conn_close():
    """Checks if connection close header will close the connection after req
  """
    with RequestManager() as ch:
        ch.send_get(url="/", headers={"Connection": "close"})
        res = ch.read_get()
        assert res["headers"]["Connection"] == "close"
        time.sleep(2)  # wait for server to process
        assert ch.is_socket_closed() == True
예제 #20
0
def test_200_bin():
    """Checks if it can fetch an existing bin file
  """
    with RequestManager() as ch:
        r = b"GET /kitten.jpg HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
        ch.send(r)
        res = ch.read_get()
    assert res["status_code"] == 200
    assert res["headers"]["Content-Type"] == "image/jpeg"
    assert res["body"] == kitten
예제 #21
0
def test_200_text():
    """Checks if it can fetch an existing text/html file
  """
    with RequestManager() as ch:
        r = b"GET /index.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
        ch.send(r)
        res = ch.read_get()
    assert res["status_code"] == 200
    assert res["headers"]["Content-Type"] == "text/html"
    assert res["body"] == root_index
예제 #22
0
def test_get_index():
    """Checks if /subdir/ is requested, it returns /subdir/index.html
  """
    with RequestManager() as ch:
        r = b"GET /subdir1/ HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
        ch.send(r)
        res = ch.read_get()
    assert res["status_code"] == 200
    assert res["headers"]["Content-Type"] == "text/html"
    assert res["body"] == subdir1_index
예제 #23
0
def test_seq_requests():
    """Checks if server can handle sequential requests
  """
    r1 = b"GET /index.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
    r2 = b"GET /subdir1/ HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
    with RequestManager() as ch:
        ch.send(r1 + r2)
        res1 = ch.read_get()
        res2 = ch.read_get()
    assert res1["body"] == root_index
    assert res2["body"] == subdir1_index
예제 #24
0
def test_large_request():
    """Checks if a request larger than 8KB results in 400 & close
  """
    params = {}
    for i in range(8000):
        params["key" + str(i)] = "value" + str(i)
    with RequestManager() as ch:
        ch.send_get(url="/", headers=params)
        res = ch.read_get()
        time.sleep(2)  # wait for server to process
        assert ch.is_socket_closed() == True
    assert res["status_code"] == 400
예제 #25
0
def test_seq_400():
    """Checks if server can handle a malformed requests in a sequence of requests.
     Expected behaviour is to return 400 when a bad req is encountered and close
     the connection discarding any request in the pipeline.
  """
    r1 = b"GET /index.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
    r2 = b"GETT /subdir1/ HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"  # malformed
    r3 = b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
    with RequestManager() as ch:
        ch.send(r1 + r2 + r3)
        time.sleep(1)
        # oddly, the client parser had a bug, so just check if conn closed
        # I've checked manually that the data returned is indeed as expected
        assert ch.is_socket_closed() == True
예제 #26
0
 def __init__(
     self,
     host: str,
     port: int,
     loop: asyncio.AbstractEventLoop = None,
     db_config: Dict = DATABASES["default"],
 ):
     self.host = host
     self.port = port
     self.req_man = RequestManager(db_config=db_config)
     if loop is None:
         self.loop = asyncio.get_event_loop()
     else:
         self.loop = loop
예제 #27
0
def test_malformed_request():
    """Checks if malformed request results in 400 & closed conn
  Two cases - 1. issue in request line, 2. issue in request headers
  """
    with RequestManager() as ch:  # wrong verb
        ch.send(b"GETT /index.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")
        res = ch.read_get()
        time.sleep(1)  # wait for server to close
        assert ch.is_socket_closed() == True
    assert res["status_code"] == 400

    with RequestManager() as ch:  # wrong uri format
        ch.send(b"GET index.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")
        res = ch.read_get()
        time.sleep(1)  # wait for server to close
        assert ch.is_socket_closed() == True
    assert res["status_code"] == 400

    with RequestManager() as ch:  # wrong headers format
        ch.send(b"GET /index.html HTTP/1.1\r\nHo st: 127.0.0.1\r\n\r\n")
        res = ch.read_get()
        time.sleep(1)  # wait for server to close
        assert ch.is_socket_closed() == True
    assert res["status_code"] == 400
    def create_batch_order(self,symbol,type,amount,price=None,stopprice=None,operator=None):
        account_id = self.spot_account_id
        method = 'POST'
        path = '/v1/order/batch-orders'
        url = self.BASE_URL + path

        params = {
            'SignatureMethod': 'HmacSHA256',
            'SignatureVersion': '2',
            'AccessKeyId': self.Access_Key,
            'Timestamp': self.utc_now(),
        }
        params["Signature"] = self.generate_signature(method, params, url)
        params = urllib.parse.urlencode(params)
        total_list=[]
        if stopprice is None:
            for i in range(len(amount)):
                data = {}
                data['account-id'] = account_id
                data['symbol'] = symbol
                data['type'] = type
                data['amount'] = str(amount[i])
                data['price'] = str(price[i])
                #data['operator'] = operator
                #data = json.dumps(data, separators=(',', ':'))
                total_list.append(data)
            total_list = json.dumps(total_list)

        elif stopprice is not None:
            for i in range(len(amount)):
                data = {}
                data['account-id'] = account_id
                data['symbol'] = symbol
                data['type'] = type
                data['amount'] = str(amount[i])
                data['price'] = str(price[i])
                data['stop-price'] = str(stopprice[i])
                #data['operator'] = operator
                #data = json.dumps(data, separators=(',', ':'))
                total_list.append(data)
            total_list=json.dumps(total_list)
        my_request = Request(
            method=method,
            url=url,
            data=total_list,
            params=params
        )
        return RequestManager().send_request(my_request, self.is_proxies)
 def cancel_order_by_id(self,order_id):
     method = 'POST'
     path = '/v1/order/orders/'+order_id+'/submitcancel'
     url = self.BASE_URL + path
     params = {
         'SignatureMethod': 'HmacSHA256',
         'SignatureVersion': '2',
         'AccessKeyId': self.Access_Key,
         'Timestamp': self.utc_now(),
     }
     params["Signature"] = self.generate_signature(method, params, url)
     params = urllib.parse.urlencode(params)
     my_request = Request(
         method=method,
         url=url,
         # data=data,
         params=params
     )
     return RequestManager().send_request(my_request, self.is_proxies)
예제 #30
0
    def crawl(self, key=None, force=False):

        if key is None:
            raise ValueError('No key specified')

        filename = self.schema.local(key)
        url = self.schema.url(key)
        
        if not force and path.exists(filename):
            return True
    
        response = RequestManager.get(url)
        if response.status_code != 200:
            return False
    
        content = response.content
        with open(filename, 'w+') as f:
            f.write(content)
            return True