SHARE
TWEET

Untitled

a guest Dec 10th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Moe
  2. from flask import Flask, request, Response
  3. import http.client, mimetypes, json, time
  4.  
  5. app = Flask(__name__)
  6.  
  7. @app.route('/iptolocation', methods=['GET'])
  8. def iptoloc():
  9.     time_start=time.time()
  10.     if 'ip' in request.args:
  11.         ip = str(request.args['ip']).strip()
  12.         checkstr = ip.replace(".","")
  13.         try:
  14.             int(checkstr)
  15.             conn = http.client.HTTPSConnection("api.techet.net")
  16.             payload = ''
  17.             headers = {
  18.               'Authorization': ' Basic V2hKajBDamVIWGtBb2p1dURHa2s6TzFIN0tpSFJnSWxLcDBKaGZTRlhGYnhtUW9JU1ZsMjhybVF1bmVlUA==',
  19.               'User-Agent': ' Dalvik/2.1.0 (Linux)',
  20.               'Connection': ' Keep-Alive'
  21.             }
  22.             conn.request("GET", "/a1/iptoloc?final_ip=%s"%(ip), payload, headers)
  23.             res = conn.getresponse()
  24.             data = res.read()
  25.             data= data.decode("utf-8")
  26.             data = json.loads(data)
  27.             data = data['locations'][0]
  28.             time_end=time.time()
  29.             exe_time=time_end-time_start
  30.             result = {"code":200, 'status': 'OK', 'result': data, 'timelapse':exe_time}
  31.             return Response(json.dumps(result), status=200, mimetype='application/json')
  32.         except:
  33.             time_end=time.time()
  34.             exe_time=time_end-time_start
  35.             error = {"code":406 , 'status': 'Not Acceptable' ,"reason": 'The \'ip\' param(s) contain string or empty',"tips": 'Check your param(s)','timelapse':exe_time}
  36.             return Response(json.dumps(error), status=406, mimetype='application/json')
  37.     else:
  38.         time_end=time.time()
  39.         exe_time=time_end-time_start
  40.         error = {"code":406 , 'status': 'Not Acceptable' ,"reason": 'No param(s) \'ip\' found', "tips": 'Check your param(s)','timelapse':exe_time}
  41.         return Response(json.dumps(error), status=406, mimetype='application/json')
  42.  
  43. if __name__ == "__main__":
  44.   app.run(host="0.0.0.0",port="8080",debug=True)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top