Today we will continue to share Python interview questions. Are you ready?
Network Programming
1. Describe the OSI Layer 7 protocol
Network transport protocol, which artificially divides the different stages of network transport into different layers.
The seven layers are divided into application layer, presentation layer, session layer, transmission layer, network layer, data link layer and physical layer.
The five layers are divided into application layer, transmission layer, network layer, data link layer and physical layer.
Physical layer: Physical devices such as network cables and cables Data link layer: Mac address Network layer: IP address Transport layer: TCP and UDP Application layer: FTP, Email, and WWW
2. Three handshakes and four waves
All occur at the transport layer
Three handshakes:
TCP is a host-to-host transmission control protocol that provides reliable connection services and uses the three-way handshake to confirm the establishment of a connection.
TCP flag bit (bit code), there are six identifiers :SYN(synchronous establishment) ACK(Acknowledgement) PSH(Push transmission) FIN(Finish) RST(reset reset) URG(Urgent urgent)
Sequence number = Acknowledge number
First handshake: Host A sends packets with bit code SYN=1 and seQ number=1234567 to the server and enters the SYN_SEND state. Host B knows from SYN=1 that host A requires to establish an online connection.
Second handshake: After receiving the request, host B confirms the online information, sends ACK number=(SeQ +1 of host A), SYN =1, ACK =1, randomly generates A packet with SEQ =7654321, and enters the SYN_RECV state.
Third handshake: After receiving the packet, host A checks whether the ACK number is correct, that is, the seQ number+1 sent for the first time and the bit code ACK is 1. If yes, host A will send ack number=(SeQ +1 of host B), ACK =1. Host B receives the message and confirms that the SEQ value and ACK value are 1. If the connection is ESTABLISHED, both hosts enter the ESTABLISHED state.
After completing the three-way handshake, host A and host B begin to transmit data.
Four waves:
Because the TCP connection is full-duplex, each direction must be closed separately. The principle is that a party can send a FIN to terminate the connection in that direction when it has finished sending its data. Receiving a FIN only means that there is no data flow in that direction, and a TCP connection can still send data after receiving a FIN. The party that closes first performs an active shutdown and the other party performs a passive shutdown.
- Server A sends A FIN to shut down data transfer from server A to server B.
- Server B receives the FIN and sends back an ACK with the received FIN number plus one. As with the SYN, a FIN takes a sequence number.
- Server B closes the connection with server A and sends A FIN to server A.
- Server A sends an ACK packet for confirmation and sets the sequence number of the ACK packet to 1.
3. What are C/S and B/S architectures
B/S is also known as the browser/server mode. Various websites, jupyter Notebook, etc.
Advantages: zero installation, simple maintenance, good sharing.
Disadvantages: Poor security, insufficient personalization.
C/S is also known as client/server mode. Such as wechat client, Oracle client and so on.
Advantages: good security, fast data transmission, stable.
Disadvantages: It has requirements on the PC operating system. When there are many clients, the server load is heavy.
4. Differences between TCP and UDP
Both TCP and UDP are transport layer protocols in the OSI model. TCP provides reliable traffic, while UDP is often used to broadcast and detail traffic to applications.
UDP does not provide complex control mechanisms and uses IP to provide connectionless communication services.
TCP fully realizes various control functions of data transmission, including retransmission control of packet loss and sequential control of subcontracting out of order.
TCP applications: FTP transmission, point-to-point SMS, etc.
UDP applications: media streaming.
5. LAN and WAN
A Wide Area Network (WAN) is also called a Long Haul Network. Usually across a large physical range, covering a range from tens of kilometers to thousands of kilometers, it can connect multiple cities or countries, or across several continents and can provide long-distance communication, forming an international long-range network.
A Local Area Network (LAN) is a computer group consisting of multiple interconnected computers in a certain Area. Usually within a few thousand meters. LAN can realize file management, application software sharing, printer sharing, working group scheduling, E-mail and fax communication services and other functions. Lans are closed and can consist of two computers in an office or thousands of computers in a company.
6. The arp protocol
Address Resolution Protocol (ARP) is used to map IP addresses to MAC addresses, that is, ask for the MAC Address of the target IP Address.
7. What is a socket? This section describes the TCP socket communication flow.
Socket is the encapsulation of TCP/IP protocol, and its appearance only makes it more convenient for programmers to use TCP/IP protocol stack. Socket itself is not a protocol, it is the application layer and TCP/IP protocol family communication intermediate software abstraction layer, is a set of call interface (TCP/IP network API functions).
“TCP/IP is just a stack of protocols. Like the operating system, it must be implemented and provided with an external interface. This is similar to the standard programming interface provided by the operating system, such as win32 programming interface. TCP/IP also provides an interface that programmers can use to do network development. This is the Socket programming interface.”
Server:
import socket
import threading
def tcplink(sock, addr):
print('Accept new connection from %s:%s... ' % addr)
sock.send(b'Welcome! ')
while True:
data = sock.recv(1024)
time.sleep(1)
if not data or data.decode('utf-8') = ='exit':
break
sock.send(('Hello, %s! ' % data.decode('utf-8')).encode('utf-8'))
sock.close()
print('Connection from %s:%s closed.' % addr)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# monitor port:
s.bind(('127.0.0.1', 9999))
s.listen(5)
print('Waiting for connection... ')
while True:
Accept a new connection:
sock, addr = s.accept()
Create a new thread to handle TCP connections:
t = threading.Thread(target=tcplink, args=(sock, addr))
t.start()
Copy the code
Client:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Create a connection:
s.connect(('127.0.0.1', 9999))
# Receive a welcome message:
print(s.recv(1024).decode('utf-8'))
for data in [b'Michael', b'Tracy', b'Sarah'] :# send data:
s.send(data)
print(s.recv(1024).decode('utf-8'))
s.send(b'exit')
s.close()
Copy the code
Examples come from Liao Xuefeng’s official website
8. Describe the differences between processes, threads, and coroutines and their application scenarios
Process is a program with a certain independent function on a data set of a running activity, process is a system for resource allocation and scheduling an independent unit. Each process has its own independent memory space, and different processes communicate with each other through interprocess communication.
A thread is an entity of a process. It is the basic unit of CPU scheduling and dispatch. It is a smaller unit that can run independently. A thread has virtually no system resources of its own, only a few resources that are essential to running (such as a program counter, a set of registers, and a stack), but it can share all the resources owned by a process with other threads that belong to the same process.
Coroutines are lightweight threads in user mode, and the scheduling of coroutines is completely controlled by users. Coroutines have their own register context and stack.
Multi-process: Intensive CPU tasks that require full use of multi-core CPU resources (servers, lots of parallel computing).
Disadvantages: High communication cost and switching overhead between multiple processes.
Multithreading: Intensive I/O tasks (network I/O, disk I/O, database I/O) use multithreading appropriately.
Disadvantages: The same time slice can only run one thread, can not achieve high parallelism, but can achieve high concurrency.
Coroutines: Also known as microthreads, perform multiple tasks on a single thread with minimal overhead by switching between functions. No scheduling through the operating system, no process, thread switching overhead.
Disadvantages: Single threaded execution, poor performance when processing cpu-intensive and local disk IO. Processing network I/O performance is still relatively high.
The return of multithreaded requests is disordered, and whichever thread has data to return is processed, while the data returned by coroutines is ordered.
9. How to use thread pools and process pools
The function of a pool is to limit the number of processes or threads started. When the number of concurrent tasks far exceeds the capacity of the computer, that is, it is impossible to start too many processes or threads at one time, the concept of pool should be used to limit the number of processes or threads opened to the range that the computer can bear.
Multiple processes
from multiprocessing import Pool
import os
import time
import random
def long_time_task(name):
print('Run task %s (%s)... ' % (name, os.getpid()))
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
print('Task % S runs %0.2f seconds.' % (name, (end - start)))
def test_pool():
print('Parent process %s.' % os.getpid())
p = Pool(4)
for i in range(5):
p.apply_async(long_time_task, args=(i,))
print('Waiting for all subprocesses done... ')
p.close()
p.join()
print('All subprocesses done.')
if __name__ == '__main__':
test_pool()
Copy the code
output
Parent process 32432.
Waiting forall subprocesses done... Run task 0 (15588)... Run task 1 (32372)... Run task 2 (12440)... Run task 3 (18956)... Task 2 runs 0.72 seconds. Run Task 4 (12440)... Task 1 runs 1.21 seconds. Task 0 runs 3.00 seconds. Task 4 runs 2.95 seconds. All subprocesses done.Copy the code
Apply_async (func [, args [, KWDS]]) : Call func in non-blocking mode (parallel execution, blocking mode must wait for the previous process to exit before the next process can execute), args is the list of arguments passed to func, KWDS is the list of keyword arguments passed to func; Close () : Closes the Pool so that it can no longer accept new tasks. Terminate () : Immediately terminate whether the task is complete or not; Join () : the main process blocks, waiting for the child process to exit, must be used after close or terminate;
It can also be implemented using the functionality provided by the concurrent.futures module
def test_future_process():
print('Parent process %s.' % os.getpid())
p = ProcessPoolExecutor(4)
for i in range(5):
p.submit(long_time_task, i)
p.shutdown(wait=True)
print('Finish')
if __name__ == '__main__':
# test_pool()
test_future_process()
Copy the code
output
Parent process 29368. Run task 0 (32148)... Run task 1 (31552)... Run task 2 (24012)... Run task 3 (29408)... Task 2 runs 0.52 seconds. Run Task 4 (24012)... Task 1 runs 1.81 seconds. Task 0 runs 1.83 seconds. Task 4 runs 1.69 seconds. FinishCopy the code
multithreading
def sayhello(a):
print("hello: " + a)
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
print('Task % S runs %0.2f seconds.' % (a, (end - start)))
def test_future_thread():
seed = ["a"."b"."c"."d"]
start = time.time()
with ThreadPoolExecutor(3) as executor:
for i in seed:
executor.submit(sayhello, i)
end = time.time()
print("Thread Run Time: " + str(end - start))
Copy the code
output
B hello: c Task a runs 0.40 seconds. D Task B runs 0.56 seconds. Task D runs 1.70 seconds. Task C runs 2.92 seconds. Thread Run Time: 2.9195945262908936Copy the code
As you can see, only three tasks are executing at the same time because you created a thread pool with a limit of 3.
10. How do processes communicate with each other
def write(q):
print("Write (%s), parent process is (%s)" % (os.getpid(), os.getppid()))
for i in "Python":
print("Put %s to Queue" % i)
q.put(i)
def read(q):
print("Read (%s), parent process is (%s)" % (os.getpid(), os.getppid()))
for i in range(q.qsize()):
print("Read gets message from Queue: %s" % q.get(True))
def test_commun():
print("(%s) start" % os.getpid())
q = Manager().Queue()
pw = Process(target=write, args=(q, ))
pr = Process(target=read, args=(q, ))
pw.start()
pr.start()
pw.join()
pr.terminate()
Copy the code
output
(23544) start write(29856), The parent process is (23544) Put P to Queue Put y to Queue Put T to Queue Put H to Queue Put O to Queue Put N to Queueread(25016), the parent process is (23544)readReceived message from Queue: PreadGet message from Queue: yreadGet message from Queue: treadGet message from Queue: hreadMessage received from Queue: oreadMessage received from Queue: nCopy the code
Python’s Multiprocessing module wraps the underlying mechanism, providing Queue, Pipes, and other ways to exchange data.
11. Process and thread locks
Process lock: to control the access of multiple processes in the same operating system to a shared resource, but because of the independence of the program, each process is unable to control the access of other processes to resources, but can use the semaphore control of the local system.
Semaphore, sometimes called a Semaphore, is a facility used in multithreaded environments to ensure that two or more critical pieces of code are not called concurrently.
Thread locking: When multiple threads modify a shared data at the same time, synchronization control is required. Thread synchronization ensures that multiple threads can safely access competing resources (global content). The simplest synchronization mechanism is the use of mutex.
When a thread wants to change the shared data, it locks it first. At this time, the resource is in the locked state, and other threads can change the resource until the thread changes the resource state to the unlocked state, that is, the resource is released, and other threads can lock the resource again. The mutex ensures that only one thread enters the write operation at a time. Thus ensuring the security of data under multithreading.
12. What are concurrency and parallelism
Parallelism: Multiple CPU cores, so that different programs are allocated to different cpus to run. Multiple programs can be executed simultaneously.
Concurrency: a single CPU core that can run only one program at a time in a time slice, or serial if multiple programs need to be run.
13. Threading. Local
A ThreadLocal is called a thread-local variable, and a ThreadLocal creates a copy of each variable. Each thread can access its own internal copy of the variable, which is invisible to other threads and does not affect other threads after modification.
14. What is domain name resolution
Domain name resolution refers to resolving a domain name into an IP address. In reverse lookup, the DNS server searches for the corresponding domain name.
The DNS is a Domain Name System that assigns Domain names and IP addresses to hosts on the Internet. When a user uses a domain name address, the system automatically converts it into an IP address.
15. What is LVS and what does it do
LVS is short for Linux Virtual Server. It is a Virtual Server cluster system, namely load balancing Server.
LVS work in NAT mode, TUN mode, and DR mode.
16. What Nginx does
Nginx main functions: 1, reverse proxy 2, load balancer 3, HTTP server (including static and static separation) 4, forward proxy
Forward proxy: In some cases, proxy users need to manually set the IP address and port number of the proxy server to access the server.
Reverse proxy: Is used to proxy the server, the proxy to access the destination server. The proxy server accepts the request, forwards it to the server on the internal network (clustering), and returns the results from the server to the client, at which point the proxy server behaves as a server.
The load balancing server is similar to LVS and the HTTP server is similar to Tomcat.
17. Keepalived and HAProxy
HAProxy provides high availability, load balancing, and TCP – and HTTP-based application proxies.
Keepalived is a cluster management service that ensures high cluster availability. It functions like heartbeat to prevent a single point of failure.
18. What is RPC
RPC refers to remote procedure call, that is, two servers A and B, one application deployed on server A, want to call the function/method provided by the application on server B, because they do not have the same memory space, they cannot call directly, so they need to express the call semantics and transfer the call data over the network.
19. The process of entering a web address from the browser to the page that displays it
- The browser uses the DNS server to find the IP address corresponding to the domain name
- The browser sends an HTTP request to the web server corresponding to the IP address
- The Web server receives the HTTP request and returns the response to the browser
- The browser receives the response and renders the page
20. What is CDN
The full name of CDN is Content Delivery Network. CDN is a content distribution network built on the network. Relying on the edge servers deployed in various places and through the function modules of load balancing, content distribution and scheduling of the central platform, users can obtain the content they need nearby, reduce network congestion and improve user access response speed and hit ratio. The key technologies of CDN mainly include content storage and distribution.
Databases and Frameworks
21. List common databases
Relational databases: MySQL, Oracle, SQLServer, SQLite, DB2
Non-relational databases: MongoDB, Redis, HBase, Neo4j
22. Three paradigms of database design
To build a scientific, standardized database, we need to meet some norms to optimize the way data is stored. In a relational database, these norms can be called paradigms.
First normal form: R is said to satisfy first normal form when all properties of the relational pattern R cannot be decomposed into more basic data units, abbreviated as 1NF.
All properties of the relational schema R can no longer be decomposed
Second normal form: if the relational pattern R satisfies the first normal form and all non-primary attributes of R are completely dependent on each candidate key attribute of R, R is said to satisfy the second normal form, abbreviated as 2NF.
Non-primary attributes depend on each key attribute
Three normal forms: suppose R is a relational mode satisfying the conditions of the first normal form, and X is any attribute set of R. If X is non-transitive and depends on any candidate keyword of R, R is said to satisfy the third normal form, denoted as 3NF.
Data cannot have transitive relationships, that is, each attribute has a direct relationship to the primary key rather than an indirect relationship
23. What are database transactions
Transaction is the basic unit of concurrency control. A transaction is a sequence of operations that are either performed or not performed, and it is an indivisible unit of work.
In a relational database, a transaction can be a SINGLE SQL statement, a set of SQL statements, or an entire program.
Four attributes: atomicity, consistency, isolation, and persistence.
MySQL > select * from ‘MySQL’
MySQL currently has the following index types:
- Normal index
- The only index
- The primary key index
- Composite index
- The full text indexing
25. One-to-many and many-to-many application scenarios in database design
Examples of one-to-one relationships: one student for one student profile material, or a unique ID number for each person.
Example of one-to-many relationship: A student belongs to only one class, but a class has multiple students.
Example of a many-to-many relationship: A student can choose more than one course and have more than one student in one course.
26. Briefly describe triggers, functions, views, and stored procedures
Trigger: A trigger is a special stored procedure that is a block of code that the database automatically executes during INSERT, UPDATE, or DELETE operations.
Functions: The database provides many built-in functions, you can also customize the function, SQL logic.
View: A view is a virtual table formed by query results. It is a projection of the table through certain operations.
Stored procedure: Encapsulates a piece of code that can be executed by calling the stored procedure. (Calling it again after the first compilation does not need to be compiled again, which is more efficient than executing SQL statements one by one.)
27. Common SQL statements
DML (Data Manipulation Language)
- SELECT – Retrieves data from a database table
- UPDATE – Updates data in a database table
- DELETE – Deletes data from a database table
- INSERT INTO – Inserts data INTO a database table
DDL (Data Definition Language)
- CREATE DATABASE – Creates a new DATABASE
- ALTER DATABASE – Changes the DATABASE
- CREATE TABLE – Creates a new TABLE
- ALTER TABLE – ALTER a database TABLE
- DROP TABLE – Deletes a TABLE
- CREATE INDEX – CREATE INDEX (search key)
- DROP INDEX – Drops an INDEX
28. Differences between primary and foreign keys
Primary and foreign keys are defined primarily to maintain the integrity of a relational database
A primary key is a unique identifier that identifies a record. It can’t be repeated. It can’t be empty.
Foreign keys are used to associate with another table. Is a field that identifies a record in another table and is used to maintain data consistency.
A primary key | A foreign key | The index | |
---|---|---|---|
define | Uniquely identifies a record. It cannot be repeated and cannot be empty | A foreign key of a table is the primary key of another table, and a foreign key can be repeated or null | This field has no duplicate value, but it can be null |
role | Used to ensure data integrity | Used to establish relationships with other tables | Speed up query sorting |
The number of | There can only be one | Can have multiple | Can have multiple |
29. How do I enable slow log query of MySQL
- Slow_query_log = ON long_query_time = 2 # Slow_query_log = ON long_query_time = 2 #
- SET GLOBAL slow_query_log = ‘ON’; SET GLOBAL long_query_time = 2;
MySQL database backup command
Mysqldump -u username -p database name > export file name
31. Difference between char and varchar
Char: It is convenient to store data of fixed length. The index on a char field is efficient. The length must be defined in parentheses and can have default values, such as char(10).
Varchar: stores variable length data, but not as efficiently as CHAR. The length must be defined in parentheses and can have a default value.
32. Left-most prefix rule
Mysql > create index (s) with left-most prefix (s)
If there is a 2-column index (col1,col2), then the index on (col1) and (col1,col2) has been created.
If there is a 3-column index (col1,col2,col3), then the index (col1), (col1,col2,col3) has been created.
33. An index cannot be hit
- Using the OR keyword will result in an index failure
- Left leading query will result in index failure, such as like ‘%a’ or like ‘%a%’
- Full-value matching invalidates a single-column index whose index column is null, and invalidates a composite index whose index column is null
- If we have 4 columns A, B, C, and D, we create a composite INDEX INDEX(
a
.b
.c
.d
A, ab, ABC, abcd, abCD, abCD, abCD, abCD, abCD, abCD - A cast will invalidate an index
- Negative query conditions cause indexes to be unavailable, such as NOT IN,NOT LIKE,! = etc.
- If mysql estimates that a full table scan is faster than an index, then the index is not used
34. Database read and write separation
Read/write separation is a common database architecture in which a database is divided into master/slave databases. One master database is used to write data, and multiple slave databases are used to read data. Data is synchronized between the master and slave databases through some mechanism.
35. Separate databases and tables
Database horizontal segmentation is a common database architecture. It is a database segmentation architecture based on algorithms. A horizontal shard of each database in the cluster is often referred to as a “shard.” Data in each shard does not overlap, but all the data in the shard is combined to form the whole data.
Horizontal segmentation is divided into sub-table in the database and sub-table in the database. According to the internal logical relationship of the data in the table, the same table is dispersed to multiple databases or multiple tables according to different conditions. Each table contains only part of the data, so that the amount of data in a single table becomes smaller and the distributed effect is achieved.
36. Redis vs. memcached
- Both Redis and memcached store data in memory. Both are in-memory databases. But memcached can also be used to cache other things, such as images, videos, and so on.
- Redis not only supports simple K/V type data, but also provides the storage of list, set, hash and other data structures.
- Distributed Settings, can do one master many slave or one master one slave.
- The storage data is secure. When memcached is down, the data is completely lost. Redis can be saved to disk periodically (persistent).
- In disaster recovery, data cannot be recovered after memcached is down. Lost redis data can be recovered through AOF.
37. Redis database default number of db and its function
Redis has 16 databases by default, and data in each database is isolated so that when storing data, you can specify different data to be stored in different databases. And only a single machine, if it is a cluster, there is no concept of database.
38. What persistence strategies does Redis have
RDB persistence: Is the persistence of Reids periodically dumping database records in memory to disk
AOF (Append only File) persistence: Operation logs of Reids are appended to files
39. Expiration policies supported by Redis
There are three expiration strategies in general
- Periodic deletion When you set the expiration time of a key, you can create a timer for the key and delete the key when the expiration time is up
- Lazy deletion If the key is expired, the system does not delete it. Each time it obtains a key from the database, the system checks whether the key is expired. If the key is expired, the system deletes it and returns NULL.
- Periodically Delete An expired key is deleted once in a while
Redis uses a lazy delete + periodic delete strategy
40. How to ensure that the data in REDis is hot data
To limit the amount of memory Redis occupies, Redis will load hot data into memory according to its own data elimination strategy. So, calculate the approximate memory usage of all the hotspot data, and then set the Redis memory limit.
41. Python operates on redis
Use redis third-party libraries to do this
import redis
Create a Redis connection pool
def redis_conn_pool():
pool = redis.ConnectionPool(host='redis-host', port=redis-port,
decode_responses=True, password='redis-pwd')
r = redis.Redis(connection_pool=pool)
return r
Copy the code
42. Publish and subscribe based on Redis
The subscriber
if __name__ == "__main__":
conn = redis.Redis(host=' ',
port=12143, password=' ')
ps = conn.pubsub()
ps.subscribe('chat') Subscribe messages from chat
for item in ps.listen(): # Monitor status: Bring it when it's published
if item['type'] = ='message':
print(item)
print(item['channel'])
print(item['data'])
Copy the code
The publisher
if __name__ == "__main__":
number_list = ['300033'.'300032'.'300031'.'300030']
signal = ['1'.'1'.'1'.'1']
pool = redis.ConnectionPool(host='redis-12143.c8.us-east-1-3.ec2.cloud.redislabs.com', port=12143,
decode_responses=True, password='pkAWNdYWfbLLfNOfxTJinm9SO16eSJFx')
r = redis.Redis(connection_pool=pool)
for i in range(len(number_list)):
value_new = str(number_list[i]) + ' ' + str(signal[i])
print(value_new)
r.publish("chat", value_new)
Copy the code
43. How to find a KEY in Redis efficiently
import redis
con = redis.Redis()
con.keys(pattern='key*') # * stands for wildcard
Copy the code
44. Implement FIFO, LIFO and priority queue based on REDis
class Zhan:
def __init__(self,conn):
self.conn = conn
def push(self,val):
self.conn.rpush('aaa',val)
def pop(self):
return self.conn.rpop('aaa')
class Dui:
def __init__(self,conn):
self.conn = conn
def push(self,val):
self.conn.rpush('bbb',val)
def get(self):
return self.conn.lpop('bbb')
class Xu:
def __init__(self,conn):
self.conn = conn
def push(self,val,count):
self.conn.zadd('ccc',val,count)
def get(self):
a = self.conn.zrange('ccc', 0, 0)[0]
self.conn.zrem('ccc', a)
return a
Copy the code
45. How does Redis implement master/slave replication
SLAVEOF 127.0.0.1 6380 # primary server IP, port
46. Loop over a very large list in Redis
def list_iter(name):
"""Custom incremental iteration of a Redis list :param name: name in redis, i.e., iterating the list corresponding to name: return: yield Returns list elements"""
list_count = r.llen(name)
for index in xrange(list_count):
yield r.lindex(name, index)
Copy the code
47. The function of watch command in redis
Watch is used to monitor a key at the last step of a transaction, i.e. before exec execution. If the monitored key is changed, the transaction is cancelled; otherwise, the transaction executes normally.
48. Redis distributed lock
The lock designed for Redis cluster prevents multiple tasks from modifying the database at the same time. Its essence is to set a timeout string for each host in the cluster. When more than half of the machines in the cluster are set successfully, the lock is considered successful, and there will be no second task to lock successfully until the lock expires or unlock.
49. The HTTP protocol
HyperText Transfer Protocol (HTTP) is the most widely used network Protocol on the Internet. HTTP is a standard for client – and server-side requests and responses. The client is the end user and the server is the website. Typically, an HTTP client initiates a request and establishes a TCP connection to a specified port (port 80 by default) on the server, where the HTTP server listens for and responds to the request from the client.
50. Uwsgi, differences between UWSGI and WSGI
WSGI: Web Server Gateway Interface is a specification that describes how a Web Server communicates with a Web Application. Django, Flask, etc all follow this protocol.
Uwsgi: a protocol for server and server applications that specifies how requests are forwarded to and returned to the application; Uwsgi is a wiring protocol rather than a communication protocol, and is commonly used here for data communication between uWSGI servers and other network servers.
UWSGI: Is a Web server that implements WSGI, uWSGI, HTTP, and other protocols. The function of HttpUwsgiModule in Nginx is to exchange with the uWSGI server.
51. HTTP status code
1 xx: information
2 xx: success
3xx: redirection
4xx: Client error
5xx: Server error
52. HTTP Common request methods
GET, POST, PUT, DELETE, PATCH, etc
53. Responsive layout
Responsive layout is a concept proposed by Ethan Marcotte in May 2010. In short, it means that a website can accommodate multiple terminals — rather than making a specific version for each terminal.
Implement a simple AJAX request
AJAX is a technique for updating parts of a web page without having to reload the entire page.
AJAX = asynchronous JavaScript and XML
$(function(){$('#send').click(function(){
$.ajax({
type: "GET".url: "test.json".data: {username: $("#username").val(), content: $("#content").val()},
dataType: "json".success: function(data){$('#resText').empty(); // Clear everything in resText
var html = ' ';
$.each(data, function(commentIndex, comment){
html += '<div class="comment"><h6>' + comment['username']
+ ':</h6><p class="para"' + comment['content']
+ '</p></div>';
});
$('#resText').html(html); }}); }); });Copy the code
55. Same-origin policy
The same origin policy restricts how documents or scripts loaded from the same source can interact with resources from another source. This is an important security mechanism for isolating potentially malicious files.
If both pages have the same protocol, port (if specified), and host, then both pages have the same source. We can also call it a protocol/host/port tuple, or simply a tuple. (“tuple” is a combination of things to form a whole, such as (1,2) binary and (1,2, 3) triplet.)
56. What is CORS
Cross-origin Resource Sharing (CORS) is an AJAX cross-domain Resource request method that supports modern browsers.
57. What is CSRF
CSRF (Cross-site Request Forgery), also known as one Click Attack/Session Riding (CSRF/XSRF).
58. The front end implements polling and long polling
polling
var xhr = new XMLHttpRequest();
setInterval(function(){
xhr.open('GET'.'/user');
xhr.onreadystatechange = function(){}; xhr.send(); },1000)
Copy the code
Long polling
function ajax(){
var xhr = new XMLHttpRequest();
xhr.open('GET'.'/user');
xhr.onreadystatechange = function(){
ajax();
};
xhr.send();
}
Copy the code
59. Brief MVC and MTV
MVC divides web applications into three layers, model (M), controller (C), and view (V), which are connected together in a plugin-like, loosely coupled way. The model is responsible for the business objects and database objects (ORM), the view is responsible for the interaction with the user (page), the controller (C) receives the user’s input to call the model and view to complete the user’s request.
The MTV patterns in Django are as follows: Model: the object responsible for the business object and database (ORM), Template: How the page is presented to the user, View: Responsible for the business logic and calling Model and Template when appropriate, essentially the same as MVC.
60. Idempotency of interfaces
Interface idempotence means that the results of one or multiple requests for the same operation are consistent without side effects caused by multiple clicks.
61. Advantages of the Flask framework
Simple, lightweight, expansibility, high degree of freedom.
62. What is ORM
ORM stands for Object Relational Mapping. The idea is to map data from tables in a relational database into objects, which are presented as objects, so that developers can transform operations on databases into operations on these objects.
63. The meaning of PV and UV
PV: Page view, page views or clicks, measures the number of pages a site’s users visit. Within a certain statistical period, the user will open or refresh a page for 1 time, and open or refresh the same page for many times will accumulate the page views. UV: Unique Visitor that counts the number of users visiting a site over a period of time (based on cookies).
64. Role of Supervisor
The Supervisor manages the processes by starting them as supervisor children through fork/exec, so we just need to add the path of the executable file that will manage the processes to the Supervisor configuration file.
65. Pros and cons of using ORM and native SQL
Advantages:
- Convenient use of object-oriented, clear statement;
- Effectively prevent SQL injection;
- Convenient dynamic construction statement, for different tables of the same operation using polymorphic implementation more elegant;
- To some extent, it is convenient to reconstruct the data layer
- Easy to set hook functions
Disadvantages:
- Not easy to handle complex query statements
- Performance is worse than using SQL directly
66. List some of Django’s built-in components
- Admin component: a component provided by adding, deleting, modifying and checking the corresponding data table in the model
- Model component: Responsible for manipulating the database
- Form component: generates HTML code; Data validity verification; Verification information is returned and displayed
- ModelForm component: used for database operations and also for validation of user requests
67. Enumerate ways to execute native SQL in Django
- Execute a custom SQL statement directly (similar to pymysql)
from django.db import connection
cursor = connection.cursor()
cursor.execute("SELECT DATE_FORMAT(create_time, '%Y-%m') FROM blog_article;")
ret = cursor.fetchall()
print(ret)
Copy the code
- Queryset. Extra (select={“key”: “native SQL statement “})
- Using the RAW method
- Execute the original SQL and return the model
- Depends on the model model and is mostly used for query operations
68. Differences between Cookies and sessions
Cookies are key-value pairs stored on the browser and can be used for user authentication.
Sesseion stores user session information on the server. The key value is a randomly generated string, and the value value is the content of the session. The sesseion stores each user’s random string to the user’s browser depending on cookies.
69. The role of the Beautifulsoup module
BeautifulSoup is a library of features for parsing, traversing, and maintaining a “label tree”.
url = "http://www.baidu.com/"
request = requests.get(url)
html = request.content
soup = BeautifulSoup(html, "html.parser", from_encoding="utf-8")
Copy the code
70. A brief description of Selenium modules
Selenium is a library that simulates the operation of a browser, enabling the browser to automatically load pages, get the data it needs, even take screenshots of pages, or determine whether certain actions are taking place on a web site, according to our instructions.
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
print(browser.page_source) # browser.page_source gets the entire HTML of the web page
browser.close()
Copy the code
Welcome to follow my wechat public account – Radish Mixed Soup, or scan the qr code below, we can communicate, learn and progress together!