学习一
时间:2009-04-02 来源:hkebao
1.Snakelets contains its own multithreaded web server.
2.Snakelets supports virtual hosting (based on hostnames). To enable this feature you have to edit the Virtual Host configuration
3.如果想要进行上传文件的话
<title>Snakelets Manual - Uploaded files</title><link href="manual.css" type="text/css" rel="stylesheet">
Response
Session object
2.Snakelets supports virtual hosting (based on hostnames). To enable this feature you have to edit the Virtual Host configuration
3.如果想要进行上传文件的话
<title>Snakelets Manual - Uploaded files</title><link href="manual.css" type="text/css" rel="stylesheet">
from snakeserver.webform import FormUploadedFile, FormFileUploadError
<title>Snakelets Manual - Uploaded files</title><link href="manual.css" type="text/css" rel="stylesheet">
The following attributes are defined on that object:
attribute description name form field name file file object to read file contents from filename filename (the base name; i.e. without any path) mimeType mime type of the uploaded file (such as 'image/gif') disposition disposition (such as 'form-data') dispositionOptions disposition options (dict) typeOptions type options (dict)
Don't forget the maxPOSTsize attribute of the Request object. It is 200000
(200 Kb) by default, if you need bigger POSTs, increase this value
(request.setMaxPOSTsize() ) before calling Request.getForm(). If a form upload
is bigger than maxPOSTsize, you will get a
snakeserver.webform.FormFileUploadError exception
4.request对象
Method | description |
---|---|
getServerSoftware() | server software version (string) |
getSnakeletsVersion() | snakelets version (string) |
getServerIP() | server IP address (string). Don't use it in an URL, this screws up the virtual hosting feature. |
getServerName() | published server hostname (string). Taken from the current virtualhost that handles the request. Which can be different from the webapp's owning vhost, because of vhost aliasing. This server name is safe to use in URLs, the virtual hosting will work nicely. |
getRealServerName() | real (internal) server hostname (string) |
getServerProtocol() | supported HTTP level (string, "HTTP/1.0") |
getServerPort() | socket port of the server (int) |
getRequestURL() | the original full request URL (without hostname and port), for example: "/page/test.cgi?arg=34" Use with the getBaseURL method to obtain a complete URL. |
getRequestURLplain() | like getRequestURL, but also without any query args. Use with the next method (getBaseURL) to obtain a complete URL without query args. |
getBaseURL() | the base URL of the server. Example: "http://desertfish.xs4all.nl:9080" |
getPathInfo() | any additional URL path components after the snakelet URL. Example: when url is 'snoop.sn/foo/bar?arg=value', it returns "/foo/bar". (Note: this is always empty when you use a fnmatch URL pattern for your snakelet!) (this string is not url-escaped) |
getMethod() | the HTTP method used ("GET" or "POST") |
getQuery() | the query args of the URL, example: "arg=value&name=foo%21". (this string is still url-escaped) |
getFullQueryArgs() | all the query args including path info and command, example: "/zip/zap?delete&arg=value&name=foo%21" (note: this is not the full URL! You can get that one from the Snakelet) (this string is still url-escaped) |
getRemoteHost() | hostname of the remote host (string) |
getRemoteAddr() | IP address of the remote host (string) |
getRealRemoteAddr() | the 'real' IP address of the remote host (use this if you are running via Apache Proxy module) |
getContentType() | the content-type of the request (string) |
getContentLength() | content length of the request (int) |
getUserAgent() | browser ID string of the client's browser, or '' (empty string) |
getReferer() | the referring URL (that is: the url we came from), or '' (empty string) |
getCookie() | raw cookie information (comma separated string) |
getCookies() | parsed cookies (mycookie.SimpleRequestCookie object, this is a dict, which maps cookie names to a list of string values) |
clearCookies() | erases all cookie information from the request (not from the client!) |
getInput() | request input stream (socket/file) |
getArg() | URL argument. Example: when url=snoop.sn?command&arg=name it returns "command" (this string is not url-escaped) |
setArg(arg) | reset the URL argument (getArg() ) to something new. |
getWebApp() | the current WebApp object |
getRangeStr() | the unparsed string value of the HTTP 'range' header, or '' (empty string). |
getRange() | the parsed HTTP 'range' header; a tuple: (from,to) |
getAuth() | the HTTP Authorization header value, or '' (empty string). |
getAllHeaders() | all HTTP headers (mimetools.Message object) |
getHeader(header) | value of specified HTTP header, or None if it isn't present |
getForm() | parsed form contents (a dict of {param name: value} ) The form has a utility method urlencode() that returns an url-encoded query args string like "arg=value&foo=bar" for the form's parameters. |
getField(param, default='') | value of a single form field parameter (or the provided default value -which is an empty string if not otherwise given- if the parameter doesn't exist) |
getParameter(param, default='') | value of a single form field or request context parameter (or the provided default value -which is an empty string if not otherwise given- if the parameter doesn't exist). The request form is first examined, if it does not contain the required field, the request context is examined for a matching attribute. If it too does not have it, the default is returned. |
getContext() | request context (ContextContainer object). Scope: request. unique per user and per request, destroyed after request completes |
getSession() | the session object (snakeserver.snakelet.Session object), None if there is no session |
deleteSession() | logout current user and deletes the session object. Also clears all cookie info on the request (not on the client!) |
getSessionContext() | the session context (ContextContainer object). Scope: session. unique per user, shared for all requests of this user. None if there is no session. |
getMaxPOSTsize() | the current max size of a POST request (in bytes) |
setMaxPOSTsize(numbytes) | set the maximum size in bytes of a POST request (default: 200000=200Kb). If it is larger, the server aborts the connection and the POST request fails, and a FormFileUploadError exception is raised. |
getEncoding() | the current request character encoding. None if not specified (means default). |
setEncoding(encoding) | forces the request character encoding. This is often necessary to correctly read non-ASCII characters from From Posts. Also note that returned form fields will be unicode objects (instead of regular strings) if you set the encoding. If you try to change the encoding after the request form fields have already been accessed, a ValueError will be raised. Using this method will override a defaultRequestEncoding that may be defined on the webapp. |
Response
Method | description |
---|---|
getOutput() | get the response output stream (socket/file). Notice that once you obtain the output stream, a number of methods will no longer work on the response object (more precisely: the methods that modify the HTTP headers that will be returned). So you have to take care of those first, before getting the output stream (try to call getOutput() as late as possible). The things you can no longer do after calling getOutput() include the following: setResponse, setHeader, setEncoding, setContentType, setContentDisposition, setContentLength, setCookie, delCookie. |
setContentType(type) | set the content type (mime type) of the response (default is "text/html") |
setContentDisposition(disposition) | set the content disposition (RFC 2183) of the response; you can control downloads with this. For example: setContentDisposition('attachment; filename="foobar.txt"') |
setContentLength(len) | set the content length (number of bytes of the response data). This is useful if you know the exact size of your response in advance. Note: you cannot use this together with a custom content character encoding! If you are 100% certain that you are providing the correct length in bytes (after the character encoding has been applied!) you can give a second parameter, "force=True" to set the length even if a custom encoding has been set. Note that the server often determines the correct content-length by itself. Only in the case of snakelets it's left to you. |
setEncoding(enc) | set character encoding of the response ('UTF-8' etc). You usually are required to set a specific character encoding if your page contains characters that are not in the plain ASCII character set. Notice that you must call getOutput() after this method! Set the encoding first, and get the output stream object after that. Also, you cannot use setContentLength() anymore. |
guessMimeType(filename) | possible mime type for this file (string) |
setHeader(header, value) | set a custom HTTP header |
getHeader(header) | get a custom HTTP header previously set by setHeader, returns None if header wasn't set |
setResponse(code, msg="output follows") | set the HTTP response code (int) and response message (string) |
HTTPredirect(URL) | send a HTTP 302 client-side redirect to the specified URL The URL can be absolute ("http://.../...") or relative to this host ("/.../...") server-side redirection/inclusion is done on the Snakelet interface. |
sendError(code, message=None) | send HTTP error with specified HTTP error code (int) and message (string) |
getCookies() | return cookies set for sending (Cookie.SimpleCookie object) |
setCookie(name, value, path=None, domain=None, maxAge=None, comment=None, secure=None) | Add a cookie that will be sent to the client. Be careful what you provide as path, when you don't specify it, a default path will be used, that can be quite different from what you expect (especially when you use internal page redirection). |
delCookie(name, path=None, domain=None, comment=None, secure=None) | Make the client browser delete the specified cookie. Be careful what you provide as path, if it isn't exactly the same as when you originally set the cookie, it won't be deleted! |
kill() | tries to abort the response connection |
Session object
getID() | the session ID (string). This is a rather long id that is cryptographically (i.e. very) hard to guess (it is constructed from several variables and then fed through SHA-1, a secure hash. The variables are the remote client address, the current time in string format and numerical format, and a random number). |
isNew() | if the session is brand new (this means that only the server knows about it, it has not been synchronised with the client yet). |
loginUser(user) | set logged in user object - this must be an instance of snakeserver.user.LoginUser. This class stores userid, password(hash), a user name and a set of privileges. Often this is enough. If you need more, you have to create your own subclass (don't forget to call __init__). For more information about the built-in LoginUser class, see authentication. |
logoutUser() | Logs out the current user. The session remains alive. Usually you also want to clear the session: see Request.deleteSession() |
getLoggedInUser() | get logged in user object (an instance of snakeserver.user.LoginUser, or a subclass thereof), or None. For more information about the built-in LoginUser class, see authentication. |
getContext() | the session context (ContextContainer object). Scope: session (Request.getSessionContext() is a shortcut for this) unique per user, shared for all requests of this user |
相关阅读 更多 +