SoFunction
Updated on 2024-11-21

Python Engineer Interview Questions Related to Python Web

The Python Engineer interview questions shared in this article are mainly related to Python Web for your reference as follows

1、Explain the relationship between WSGI and FastCGI?

CGIThe full name is "Common Gateway Interface" (CommonGateway Interface), a tool for HTTP servers to "talk" to programs on your or other machines that are running on a web 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 long-lived CGI, it can be executed all the time, as long as it is activated, and does not take time to fork every time (this is the most criticized fork-and-execute model of CGI). It also supports distributed computing, i.e., FastCGI programs can be executed on hosts other than the web server and receive requests from other web servers.
FastCGI is a language-independent, open extension to CGI with a scalable architecture, whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance. It is well known that repeated loading of the CGI interpreter is the main reason for the low performance of CGI. If the CGI interpreter stays in memory and is scheduled by the FastCGI process manager, it can provide good performance, scalability, Fail- Over features, and so on.
WSGIis called PythonWeb Server Gateway Interface v1.0 (Python Web Server Gateway Interface).
It is an interface between Python applications and WEB servers.
Its role is similar to that of protocols such as FCGI or FASTCGI.
The goal of WSGI is to create a simple and universally applicable interface between servers and WEB frameworks.
FlupIt is an implementation of WSGI using the Python language, and is a tool or library that can be used in Python application development.
Spawn-fcgi is a small program, the role of this program is to manage the fast-cgi process, then manage the wsgi process is also no problem, the function is similar to php-fpm.
So, in short, both WSGI and FastCGI are a kind of CGI for connecting WEB servers to applications, while WSGI refers to Python applications exclusively. While flup is an implementation of WSGI, Spawn-fcgi is a tool used to manage the flup process, which can start multiple wsgi processes and manage them.

2, explain the relationship between Django and Tornado, difference

DjangoOriginating from an online news Web site, it was released as open source in 2005.
The core components of the Django framework are:
Object-relational mapping for model creation Perfect administration interface for end-users State-of-the-art URL design Designer-friendly template language Caching system and much more!
It encourages rapid development and follows an MVC design. Django adheres to the BSD copyright, and the latest release is Django.
1.4, released on March 23, 2012 . Django's main purpose is to simple , fast development of database-driven website . It emphasizes code reuse , multiple components can be easily "plug-in" form to serve the entire framework , Django has many powerful third-party plug-ins , you can even easily develop their own toolkit . This makes Django has a strong scalability . It also emphasizes rapid development and DRY (Do Not RepeatYourself) principle.
Tornadois an open source version of the extensible non-blocking web server used by FriendFeed and its associated tools. The web framework looks a bit like or Google webapp, but includes some useful tools and optimizations to make effective use of the non-blocking server environment.
Tornado is significantly different from today's leading web server frameworks (including most Python frameworks): it's a non-blocking server, and it's pretty fast. Thanks to its non-blocking approach and 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 goal in developing this web server was to handle the real-time functionality of FriendFeed - each active user in the FriendFeed application maintains a connection to the server. (The question of how to scale the server to handle thousands of client connections.

3, explain the use of django-debug-toolbar

When developing a site with django, you can use django-debug-toolbar for debugging. Add 'debug_toolbar.' to your project's MIDDLEWARE_CLASSES.

4, explain the Django use redis cache server

In order to be able to use redis in Django, you also need to install the redis for Django plugin. Then it's configured in Django's settings. Now that the connection and configuration are complete, a simple example follows:

from  import settings 
from  import cache 
#read cache user id 
def read_from_cache(self, user_name): 
  key = 'user_id_of_'+user_name 
  value = (key) 
  if value == None: 
    data = None 
  else: 
    data = (value) 
  return data 
#write cache user id 
def write_to_cache(self, user_name): 
  key = 'user_id_of_'+user_name 
  (key, (user_name), settings.NEVER_REDIS_TIMEOUT) 

5, how to Django unit testing

Django's unit tests use python's unittest module, which uses a class-based approach to defining tests. The class is named,inherited from python.

from  import TestCase 
from  import Animal 
  
class AnimalTestCase(TestCase): 
  def setUp(self): 
    (name="lion", sound="roar") 
    (name="cat", sound="meow") 
  
  def test_animals_can_speak(self): 
    """Animals that can speak are correctly identified""" 
    lion = (name="lion") 
    cat = (name="cat") 
    ((), 'The lion says "roar"') 
    ((), 'The cat says "meow"') 

Execute all the tests in the directory (all the test*.py files): When running tests, the test program will look for all test cases (subclasses) in all the files starting with test, automatically build the test set and then run the tests.

$ python  test 

Execute the tests in the tests package under the animals project:

$ python   

Execute the test test in the animals project:

$ python  testanimals 

Execute a test case individually:

$ python   

Execute a test method individually:

$ python  .test_animals_can_speak 

Provide a path to the test file:

$ python  testanimals/ 

Wildcard test file name:

$ python  test--pattern="tests_*.py" 

Enable warnings alerts:

$ python -Wall  test 

6, explain the Http protocol

HTTP is an object-oriented protocol belonging to the application layer and is suitable for distributed hypermedia information systems due to its simplicity and fast approach.
The main features of the HTTP protocol can be summarized as follows:

  • 1. Support client/server mode.
  • 2. Simple and fast: when a client requests a service from a server, it only needs to transmit the request method and path. Commonly used request methods are GET, HEAD, POST. each method specifies a different type of contact between the client and the server. Because of the simplicity of the HTTP protocol, it makes the HTTP server's program size small, and thus the communication speed is very fast.
  • 3. Flexible: HTTP allows the transmission of any type of data object. The type being transmitted is labeled by the Content-Type.
  • 4. No connection: The meaning of no connection is to limit the processing of only one request per connection. After the server has processed the client's request and received the client's answer, it disconnects. This approach saves transmission time.
  • 5. Stateless: The HTTP protocol is stateless. Stateless means that the protocol has no memory capability for transaction processing. The lack of state means that if the previous information is needed for subsequent processing, it must be retransmitted, which may result in an increase in the amount of data transmitted per connection. On the other hand, the server responds faster when it does not need the previous information.

7, explain the Http request header and common response status code

Accept: refers to the MIME file format that the browser or other clients can accept. It can be judged according to it and return the appropriate file format.
Accept-Charset: Indicates the character encoding that the browser can accept. The default for English browsers is ISO-8859-1.
Accept-Language: Indicates the kind of language that the browser can accept, such as en or en-us, referring to English.
Accept-Encoding: Indicates the encoding that is acceptable to the browser. The encoding differs from the file format in that it is used to compress files and speed up file delivery. The browser decodes the Web response after receiving it and then checks the file format.
Cache-Control: Sets options about how requests are stored by the proxy server. Generally not used.
Connection: used to tell the server if a fixed HTTP connection can be maintained. HTTP/1.1 uses Keep-Alive as the default value so that when the browser needs multiple files (such as an HTML file and associated graphics files), it does not need to establish a connection each time.
Content-Type: the content type of the request. You can use the getContentType() method of HttpServletRequest to get it.
Cookie: The browser uses this attribute to send a cookie to the server. cookies are small bodies of data stored in the browser that can record user information related to the server and can also be used to implement session functionality.

Supplement:

The status code consists of three digits, the first of which defines the category of the response and has five possible values:
1xx: Indication message - indicates that the request has been received, continue processing
2xx: Success - indicates that the request has been successfully received, understood, accepted
3xx: redirection - further action is necessary to complete the request
4xx: Client error - request has a syntax error or request cannot be fulfilled
5xx: Server-side error - server failed to fulfill a legitimate request
Common status codes, status descriptions, and instructions:
200 OK // Client request successful
400 Bad Request // Client request has a syntax error and cannot be understood by the server.
401 Unauthorized // request is unauthorized, this status code must be used with the WWW-Authenticate header field
403 Forbidden //Server received request but refused to provide service
404 Not Found //Requested resource doesn't exist, eg: entered wrong URL
500 Internal Server Error //An unanticipated error occurred on the server.
503 Server Unavailable //The server can't process the client's request, it may be back to normal after some time.
eg:HTTP/1.1 200 OK (CRLF)

I hope this article will help you to clear your Python interview smoothly.