Python engineer face questions related to Python web

Source: Internet
Author: User
Tags mime file send cookies
This article is for you to share the Python engineers face questions are mainly related to the Python web, for your reference, the specific content as follows

1, explain the relationship between WSGI and FastCGI?
CGIThe full name is a "public Gateway Interface" (Commongateway Interface), a tool that the HTTP server "chats" with programs on your or other machines, and its programs must be run on a network server. CGI can be written in any language, as long as the language has standard input, output, and environment variables. such as PHP,PERL,TCL and so on.
FastCGILike a resident (long-live) CGI, it can be executed all the time, so long as it is activated, it will not take a moment to fork once (this is the most notorious fork-and-execute mode of CGI). It also supports distributed operations where the FastCGI program can execute and accept requests from other Web servers on hosts other than the Web server.
FastCGI is a language-independent, extensible architecture for CGI open extensions whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. As we all know, the repeated loading of CGI interpreter is the main reason of poor CGI performance, if the CGI interpreter remains in memory and accepts the FASTCGI process manager scheduling, it can provide good performance, scalability, fail-over characteristics and so on.
WSGIThe full name is: Pythonweb Server Gateway Interface v1.0 (Python Web Server gateways Interface),
It is an interface between a Python application and a WEB server.
Its role is similar to the role of protocols such as FCGI or FASTCGI.
The goal of WSGI is to build an interface between a simple and universally applicable server and a WEB framework.
Flupis an implementation of the Python language for WSGI, a tool that can be used in Python's application development or a library.
SPAWN-FCGI is a small program, the role of this program is to manage the fast-cgi process, then the management Wsgi process is also no problem, features and php-fpm similar.
So, simply put, WSGI and fastcgi are a kind of CGI that connects Web servers to applications, and WSGI refers to Python applications. While Flup is an implementation of WSGI, spawn-fcgi is a tool for managing FLUP processes that can initiate multiple WSGI processes and manage them.
2, explain the relationship between Django and Tornado, the difference
DjangoOriginated from an online news web site that was released in 2005 in the form of open source.
The core components of the Django framework are:
Object relational mapping for creating models the perfect management interface for end-user design first-class URL design designer-friendly template language caching system and so on
It encourages rapid development and follows the MVC design. Django complies with BSD copyright, the latest release is Django
1.4, released on March 23, 2012. The main purpose of Django is to develop a database-driven Web site quickly and easily. It emphasizes code reuse, multiple components can be conveniently in the form of "plug-in" to serve the entire framework, Django has many powerful third-party plug-ins, you can even easily develop their own toolkit. This makes Django highly extensible. It also emphasizes the principle of rapid development and dry (do not repeatyourself).
Tornadois an open source version of the extensible, non-blocking Web server and its associated tools used by FriendFeed. The web framework looks somewhat like web.py or Google's webapp, but in order to make efficient use of the non-blocking server environment, the web framework also contains useful tools and optimizations that are relevant.
There is a significant difference between the Tornado and the current mainstream WEB server framework, including most Python frameworks: It's a non-blocking server and it's pretty fast. Profiting from its non-blocking approach and the use of Epoll, Tornado can handle thousands of connections per second, which means that Tornado is an ideal web framework for real-time Web services. Our main purpose in developing this WEB server is to deal with the real-time functionality of FriendFeed, where every active user maintains a server connection in the FriendFeed application. (about how to scale up the server to handle the connection problems of thousands of clients.)
3, explain the use of Django-debug-toolbar
When you use the Django development site, you can use Django-debug-toolbar to debug. Add ' Debug_toolbar.middleware.DebugToolbarMiddleware ' to the project's middleware_classes within settings.py.
4, explained that Django uses the Redis cache server
To be able to use Redis in Django, you also need to install the Redis for Django plug-in. It is then configured in the Django settings. Now that the connection and configuration are complete, here's a simple example:

From django.conf import settings from Django.core.cache Import cache #read The cache user ID def read_from_cache (self, User_na ME):   key = ' user_id_of_ ' +user_name   value = Cache.get (key)   if value = = None:     data = none   else:     data = Json.loads (value)   return data #write cache user ID def write_to_cache (self, user_name):   key = ' user_id_of_ ' +user_name   

5. How to perform Django unit testing
The Django unit test uses the Python unittest module, which uses a class-based approach to define tests. The class name is Django.test.TestCase and inherits from Python's unittest. TestCase.

From django.test import TestCase from myapp.models import Animal   class Animaltestcase (TestCase):   def setUp (self ):     Animal.objects.create (name= "Lion", sound= "Roar")     Animal.objects.create (name= "Cat", sound= "Meow")     def test_animals_can_speak (self): "", "     animals that can speak is correctly identified" ""     lion = Animal.objects.get (name= "Lion")     cat = Animal.objects.get (name= "cat")     self.assertequal (Lion.speak (), ' the Lion says "Roar")     

Perform all the tests in the directory (all test*.py files): When you run the test, the test program finds all the test cases (inittest) in all files that begin with test. TestCase), set up the test set automatically and then run the test.

Perform the tests in the tests package under the Animals Project:

Perform test tests in the Animals project:

Execute a test case individually:

Execute a test method individually:

To provide a path for the test file:

Wildcard test file name:

Enable warnings Reminders:

6, explain the HTTP protocol
HTTP is an object-oriented protocol belonging to the application layer, which is suitable for distributed hypermedia information System because of its simple and fast way.
The main features of the HTTP protocol can be summarized as follows:

    • 1. Support client/server mode.
    • 2. Simple and fast: When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST. Each method specifies a different type of contact between the customer and the server. Because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.
    • 3. Flexible: HTTP allows the transfer of any type of data object. The type being transmitted is marked by Content-type.
    • 4. No connection: The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.
    • 5. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.

7. Explanation of HTTP request headers and common response status codes
Accept: Refers to the MIME file format in which the browser or other customer can receive the love. The appropriate file format can be judged and returned according to it.
Accept-charset: Indicates the character encoding that the browser can accept. The default value for the English browser is iso-8859-1.
Accept-language: Indicates the type of language that the browser can accept, such as en or en-us, referring to English.
Accept-encoding: Indicates how the browser can accept the encoding. Encoding differs from file format in order to compress files and speed up file delivery. The browser decodes the Web response after it receives it, and then checks the file format.
Cache-control: Sets relevant options for the request to be stored by the proxy server. Generally not used.
Connection: Used to tell the server whether a fixed HTTP connection can be maintained. http/1.1 uses keep-alive as the default, so that when a browser needs multiple files (such as an HTML file and related graphics files), it does not need to establish a connection every time.
Content-type: The content type used for table name request. Can be obtained using the HttpServletRequest getContentType () method.
Cookie: This property is used by the browser to send cookies to the server. Cookies are small data bodies that are stored in a browser, which can record user information related to the server, and can also be used to implement conversational functions.

Add:

The status code consists of three digits, the first number defines the category of the response, and there are five possible values:
1XX: Indication information--Indicates that the request has been received and continues processing
2XX: Success-Indicates that the request has been successfully received, understood, accepted
3XX: Redirect--further action is required to complete the request
4XX: Client Error--Request syntax error or request not implemented
5XX: Server-side error-the server failed to implement a legitimate request
Common status codes, status descriptions, descriptions:
$ OK//client request succeeded
Bad Request//client requests have syntax errors and cannot be understood by the server
401 Unauthorized//request unauthorized, this status code must be used with the Www-authenticate header field
403 Forbidden//server receives request, but refuses to provide service
404 Not Found//request resource not present, eg: Wrong URL entered
Internal Server error//server unexpected errors
503 Server Unavailable//server is currently unable to process client requests and may return to normal after some time
eg:http/1.1 OK (CRLF)

Hopefully this article will help you get through the Python interview smoothly.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.