Maonyn
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Maonyn

Questions / Réponses

Le Deal du moment :
Réassort du coffret Pokémon 151 ...
Voir le deal

Vous n'êtes pas connecté. Connectez-vous ou enregistrez-vous

programme sauvegarde page html en memcache µappengine µpython µgolang

Aller en bas  Message [Page 1 sur 1]

anonymous1240


+

go :

Code:
package base

import "net/http"
import "fmt"
import "crypto/sha1"
import "io"
import "encoding/base64"
import "appengine/memcache"
import "appengine"
import "html"

func init() {
  http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {



  if _,ok := r.Header["If-None-Match"]; ok {
    w.WriteHeader(http.StatusNotModified)
    return
  }

  c := appengine.NewContext(r)

  w.Header().Set("Cache-control", "public, max-age=31536000")
  w.Header().Set("ETag", "1")

  if r.Method == "GET" {
    if r.URL.Path == "/" {
      fmt.Fprint(w, `<!doctype html><head><meta charset="utf-8" /><title>Page</title><style>textarea{width:60%;height:150px}</style></head><body><form method="post"><textarea name="content"></textarea><br><button type="submit">Envoyer</button></form></body></html>`)
    } else {

      if r.URL.Path[len(r.URL.Path)-1:] == "+" {
        if item, err := memcache.Get(c, r.URL.Path[1:len(r.URL.Path)-1]); err == memcache.ErrCacheMiss || err != nil {
            http.Redirect(w, r, "/", http.StatusSeeOther)
        } else {
            fmt.Fprintf(w, `<!doctype html><head><meta charset="utf-8" /><title>Page</title><style>textarea{width:60%;height:150px}</style></head><body><form method="post"><textarea name="content">%s</textarea><br><button type="submit">Envoyer</button></form></body></html>`,html.EscapeString(string(item.Value)))
        }

      } else {

        if item, err := memcache.Get(c, r.URL.Path[1:]); err == memcache.ErrCacheMiss || err != nil {
            http.Redirect(w, r, "/", http.StatusSeeOther)
        } else {
            fmt.Fprint(w, string(item.Value))
        }
      }

    }
  } else {
    h := sha1.New()
    io.WriteString(h, r.FormValue("content"))
    sum := h.Sum(nil)
    b := make([]byte, base64.URLEncoding.EncodedLen(len(sum)))
    base64.URLEncoding.Encode(b, sum)

    item := &memcache.Item{
      Key:  string(b[:10]),
      Value: []byte(r.FormValue("content")),
    }

    memcache.Add(c, item)

    http.Redirect(w, r, "/"+string(b[:10]), http.StatusSeeOther)
  }

}
python :

Code:
import webapp2
import cgi

from hashlib import sha1
from google.appengine.api import memcache

class MainPage(webapp2.RequestHandler):
    def get(self):
        if self.request.headers.get('If-None-Match'):
            self.response.set_status(304)
            return
        self.response.headers['Content-Type'] = 'text/html'
        self.response.headers['Cache-Control'] = 'public, max-age=31536000'
        self.response.headers['ETag'] = '1'

        if self.request.path == "/":
            self.response.write(
"""<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Page</title>
  <style>
    textarea{width:60%;height:150px}
  </style>
</head>
<body>
  <form method="post">
    <textarea name="content"></textarea><br>
    <button type="submit">Envoyer</button>
  </form>
</body>
</html>""")
        else:
            if self.request.path[-1:] == '+':
                data= memcache.get(self.request.path[1:-1])
                if data is not None:
                    self.response.write("""<!doctype html><head><meta charset="utf-8" /><title>Page</title><style>textarea{width:60%%;height:150px}</style></head><body><form method="post"><textarea name="content">%s</textarea><br><button type="submit">Envoyer</button></form></body></html>""" % cgi.escape(data))
                else:
                    self.redirect("/", False)
            else:
                data= memcache.get(self.request.path[1:])
                if data is not None:
                    self.response.write(data)
                else:
                    self.redirect("/", False)


    def post(self):
        self.response.headers['Content-Type'] = 'text/html'
        h= sha1(self.request.get('content').encode('utf-8')).hexdigest()
        memcache.add( h, self.request.get('content') )
        self.redirect("/"+h, False)
        return

app = webapp2.WSGIApplication([('/.*', MainPage)],
                              debug=True)

Revenir en haut  Message [Page 1 sur 1]

Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum