Advertisement
salahzar

sal-dns.py

Mar 16th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.19 KB | None | 0 0
  1. import webapp2
  2. import urllib
  3. #from google.appengine.ext import webapp
  4. #from google.appengine.ext.webapp.util import run_wsgi_app
  5. from google.appengine.ext import db
  6.  
  7. class Service(db.Model):
  8.   name = db.StringProperty(multiline=False)
  9.   url = db.StringProperty(multiline=False)
  10.  
  11. class MainPage(webapp2.RequestHandler):
  12.   def get(self):
  13.  
  14.      if self.request.get('type')=='add':  # Adding a new service to the DNS (You can also use Update but it won't tell you the service already exists)
  15.           param2=self.request.get('name') #the Name the service will be known by
  16.           param3=self.request.get('url') # the URL for the web service
  17.           q = db.GqlQuery("SELECT * FROM Service WHERE name = :kk",kk=param2)
  18.           count=q.count(2)
  19.  
  20.           if count==0 :  # the service doesn't exist, so add it.
  21.               if param2=="" or param3=="" :
  22.                   self.response.out.write('Error2')
  23.               else:
  24.                   newrec=Service(name=param2,url=param3)
  25.                   newrec.put()
  26.                   self.response.out.write('Added')
  27.           else:
  28.               self.response.out.write('Found')  # service already exists so announce that and do nothing
  29.  
  30.      elif self.request.get('type')=='remove': #removing a service
  31.           param2=self.request.get('name')    # the name the service is known by
  32.           q = db.GqlQuery("SELECT * FROM Service WHERE name = :kk",kk=param2)
  33.           count=q.count(2)
  34.  
  35.           if count==0 :
  36.             self.response.out.write('None') # Service wasn't found
  37.           else:
  38.             results=q.fetch(10)
  39.             db.delete(results)  # remove them all (just in case some how, some way, there is more than one service with the same name
  40.             self.response.out.write('Removed')
  41.  
  42.      elif self.request.get('type')=='update':  # update an existing service. Note this creates a new service, or updates an existing one
  43.           param2=self.request.get('name') #the Name the service will be known by
  44.           param3=self.request.get('url') # the URL for the web service
  45.           q = db.GqlQuery("SELECT * FROM Service WHERE name = :kk",kk=param2)
  46.           count=q.count(2)
  47.  
  48.           if count!=0 :  # if record already exists, remove it
  49.                 results=q.fetch(10)
  50.                 db.delete(results)  # remove them all (just in case some how, some way, there is more than one service with the same name
  51.           if param2=="" or param3=="" :
  52.               self.response.out.write('Error2')
  53.           else:
  54.               newrec=Service(name=param2,url=param3) # add record, either replacing the deleted one, or adding a new one if it never existed
  55.               newrec.put()
  56.               if count!=0 :
  57.                   self.response.out.write('Updated')
  58.               else:
  59.                   self.response.out.write('Added')
  60.  
  61.      elif self.request.get('type')=='retrieve': # get the current URL for a given service
  62.           param2=self.request.get('name')    # the name the service is known by
  63.           q = db.GqlQuery("SELECT * FROM Service WHERE name = :kk",kk=param2)
  64.           count=q.count(2)
  65.           if count==0 :
  66.                 self.response.out.write('None') # Service wasn't found
  67.           else:
  68.                 record=q.get()
  69.                 self.response.out.write(record.url) #print the URL
  70.      elif self.request.get('type')=='list': # List the existing services
  71.           q = db.GqlQuery("SELECT * FROM Service" )
  72.           count=q.count()
  73.  
  74.           if count==0 :
  75.               self.response.out.write('Empty') # Services weren't found
  76.           else:
  77.               results = q.fetch(1000)
  78.               for result in results:
  79.                  self.response.out.write(result.name+','+result.url+" ") # added url by Salahzar
  80.               self.response.out.write('END')  # Cap the list
  81.  
  82.      else: self.response.out.write('Error')
  83.  
  84.  
  85.  
  86. class Redirector(webapp2.RequestHandler):
  87.   def get(self):
  88.     service_name=self.request.path
  89.     if service_name[-1]=='/' :
  90.       service_name=service_name[1:-1] #remove leading and trailing slash
  91.     else:
  92.       service_name=service_name[1:]  # remove leading slash only
  93.     # salahzar begin for passing on the pathinfo
  94.     i=service_name.find("/")
  95.     lastpath=''
  96.     if(i>=0):
  97.        lastpath=service_name[i+1:]
  98.        service_name=service_name[0:i]
  99.     # salahzar end
  100.  
  101.     #un-escape just in case you're Kinki :p
  102.     service_name=urllib.unquote(service_name)
  103.     #self.response.out.write("#"+service_name+"#:"+lastpath)
  104.  
  105.     q = db.GqlQuery("SELECT * FROM Service WHERE name = :kk",kk=service_name)
  106.     count=q.count(2)
  107.     if count==0 :
  108.       self.response.out.write('None') # Service wasn't found
  109.     else:
  110.       record=q.get()  #get the URL we stored previously
  111.  
  112.       # salahzar in next lines added "/"+lastpath to be sure to pass on pathinfo
  113.       if self.request.query_string != '' :
  114.         self.redirect(urllib.unquote(record.url)+"/"+lastpath+'?'+self.request.query_string) # redirect to the HTTP-IN URL with arugments
  115.       else:
  116.         self.redirect(urllib.unquote(record.url)+"/"+lastpath) # redirect to the HTTP-IN URL
  117.  
  118.  
  119. app = webapp2.WSGIApplication([
  120.     ('/', MainPage),
  121.     ('/.*',Redirector)],
  122. debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement