def upload(req): # We only allow POST's if req.method is not zfrog.METHOD_POST: req.response_header("allow", "post") req.response(400, b'') return # Ask zfrog to parse incoming multipart data req.populate_multi() # Lookup the file called "file" file = req.file_lookup("file") if not file: req.response(400, b'') return zfrog.log(zfrog.LOG_INFO, "%s (%s, filename=%s)" % (file, file.name, file.filename)) # Open target file f = open(file.filename, "wb") if not f: req.response(500, b'') return # Read all data from incoming file and write it to the output file len = True while len: len, bytes = file.read(1024) zfrog.log(zfrog.LOG_INFO, "got %d bytes of data" % len) f.write(bytes) f.close() req.response(200, b'')
def json_parse(req): if req.method != zfrog.METHOD_PUT: req.response(400, b'') else: data = json.loads(req.body) zfrog.log(zfrog.LOG_INFO, "loaded json %s" % data) if data["hello"] == 123: zfrog.log(zfrog.LOG_INFO, "hello is 123!") req.response(200, "ok".encode("utf-8"))
async def async_socket(req): # Create the socket using Pythons built-in socket class. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set it to nonblocking. sock.setblocking(False) # Create a zfrog.socket with zfrog.socket_wrap(). conn = zfrog.socket_wrap(sock) # Asynchronously connect to 127.0.0.1 port 8888 await conn.connect("127.0.0.1", 8888) zfrog.log(zfrog.LOG_INFO, "connected!") # Now send the GET request msg = "GET /socket-test HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n" await conn.send(msg.encode()) zfrog.log(zfrog.LOG_INFO, "request sent!") # Read the response. data = await conn.recv(8192) zfrog.log(zfrog.LOG_INFO, "got response!") # Respond with the response from /socket-test. req.response(200, data) # Close the underlying socket, no need to close the wrapped zfrog.socket sock.close()
def page(req): zfrog.log(zfrog.LOG_INFO, "%s path is %s - host is %s" % (req, req.path, req.host)) zfrog.log(zfrog.LOG_INFO, "connection is %s" % req.connection) xframe = req.request_header("xframe") if xframe != None: zfrog.log(zfrog.LOG_INFO, "xframe header present: '%s'" % xframe) if req.method == zfrog.METHOD_POST: try: length, body = req.body_read(1024) zfrog.log( zfrog.LOG_INFO, "POST and got %d bytes! (%s)" % (length, body.decode("utf-8"))) except RuntimeError as r: zfrog.log(zfrog.LOG_INFO, "oops runtime error %s" % r) req.response(500, b'') except: zfrog.log(zfrog.LOG_INFO, "oops other error") req.response(500, b'') else: req.response_header("content-type", "text/plain") req.response(200, body) else: req.populate_get() id = req.argument("id") if id != None: zfrog.log(zfrog.LOG_INFO, "got id of %s" % id) req.response_header("content-type", "text/plain") req.response(200, "hello 1234".encode("utf-8"))
def cf_worker_configure(): zfrog.log(zfrog.LOG_INFO, "cf_worker_configure called!")
def cf_parent_configure(): # Listen on an additional interface and port zfrog.listen("127.0.0.1", "8889", "") zfrog.log(zfrog.LOG_INFO, "cf_parent_configure called!")
def onload(action): zfrog.log(zfrog.LOG_INFO, "python module onload called with %d!" % action) return zfrog.RESULT_OK
def python_validator(req, data): zfrog.log(zfrog.LOG_NOTICE, "python validator called %s" % data) return zfrog.RESULT_OK
def python_auth(req, data): zfrog.log(zfrog.LOG_NOTICE, "python auth called %s" % data) return zfrog.RESULT_OK
def kkk(req): req.populate_cookies() cookie = req.cookie("hello") if cookie is not None: zfrog.log(zfrog.LOG_INFO, "got hello with value %s" % cookie) req.response(200, b'')
def onconnect(c): zfrog.log(zfrog.LOG_INFO, "%s: py connected" % c)
def ondisconnect(c): zfrog.log(zfrog.LOG_INFO, "%s: py disconnecting" % c)