Managing FastAPI Projects with Poetry: A Step-by-Step Guide

Image
This guide will walk you through how to use Poetry to manage dependencies and structure your project in FastAPI . It includes detailed explanations of Poetry's core concepts and commands to help prevent issues that can arise during team collaboration and deployment. 1. What is Poetry? Poetry is a dependency management and packaging tool for Python. It goes beyond simply installing libraries by allowing you to clearly declare the dependencies your project needs and ensuring that all developers on the project have the same library versions. Clarity in Dependency Management : Explicitly manage your project's basic information and required libraries through the pyproject.toml file. Reproducible Builds : By locking all dependency versions in the poetry.lock file, it fundamentally prevents "it works on my machine" problems. Integrated Development Environment : It automatically creates and manages isolated virtual environments for each project and handles mo...

What is TCP? Segment Structure, Handshake, Control Bits Explained

1. What is Transmission Control Protocol (TCP) protocol?

The TCP/IP protocol has been discussed and standardized in the IETF. RFC 793 is the based standard for TCP, but it doesn't include all the details about modern TCP operations.

TCP is a transport layer protocol that provides all the features that a general process requires for reliable data transmission through an unstable and unreliable network.

It provides addressing for a process (application) in the form of TCP ports and allows devices to use these ports for the purpose of establishing connections between them. It is the most significant feature as a transport layer.

When a connection is established between devices, they can exchange data between the devices with flow control and error control mechanisms simultaneously.

Finally, a process can send data bytes to TCP as a simple stream, and TCP deals with packaging and sending the data as segments.

2. TCP Segment Structure and Header Format

TCP messages are called segments, the name describes the fact that each is a part of the entire data stream between the devices. TCP segments are flexible and are utilized for various purposes. A one field format is used for all segments, with several header fields that perform the many functions and features for which TCP is in charge.

2.1 TCP Segment format

Header minimum size is 20 bytes and the maximum is 60 bytes.

TCP segment header format structure


2.2 TCP Segment field descriptions

Field Name Size (Bits) Description
Source Port 16 The port number of the process which is sending the TCP segment on a source device.
Destination Port 16 The port number of the process which is receiving the TCP segment on a destination device.
Sequence Number 32 For regular transmissions, this is the sequence number of the first byte of data in this segment. It is used to reassemble the message at the receiving end if the segments are received out of order.
Acknowledgement Number 32 When the ACK bit is set, this segment is acting as an acknowledgement, and this field contains the sequence number the source is next expecting the destination to send.
Data Offset 4 The number of 32-bit words in the TCP Header. This indicates where the data starts; in other words, an offset from the beginning of the TCP segment.
Reserved 6 Reserved for future use, must be filled with zero.
Control Bits 6 There are six kinds of 1-bit Control Bits that regulate connection establishment, connection termination, connection abortion, flow control, mode of transfer etc. The 6 bits are described in chapter 3.1.
Window 16 This indicates the bytes of data the sender of this segment is willing to accept from the receiver at one time. This generally represents the size of the buffer assigned to accept data for this connection.
Checksum 16 It is used to preserve the TCP segment against errors in transmission. This is a checksum for data integrity protection, computed over the TCP datagram. In addition to, a special pseudo header of fields.
Urgent Pointer 16 This is used in relation with the URG control bit for priority data transfer. This field contains the sequence number of the last byte of urgent data.
Opinion Variable TCP has a common mechanism for including one or more sets of optional data in a TCP segment. Each of the options can be either one byte in length or variable in length. The first byte is the option-kind subfield, and its value defines the type of option, which indicates whether the option is just a single byte or multiple bytes at the same time.
Padding Variable If the option's field is not a multiple of 32 bits in length, necessary zeros are added to pad the header, so the size of the TCP header becomes a multiple of 32 bits.
Data Variable This is the bytes of data being sent in the segment.

3. TCP Control and Data Segment Types

One of the most significant features of TCP segments is that those are developed to carry both control information and data simultaneously. This feature decreases the number of segments sent, because a segment can execute more than one operation.

3.1 Control TCP Segments

Control bits fields of the TCP header are set to indicate the communication of control information such as manipulate connection establishment, connection termination, connection abortion, flow control, mode of transfer etc.

Subfield Id Bits Description
URG 1 Urgent pointer. This bit is responsible to be forwarded to the application layer urgently, even if there is more data left to be forwarded. This pointer notifies the receiver to process the urgent packets before any other packets. The receiver gets notified when packets have been received by the receiver.
ACK 1 Acknowledgement number. When set to 1, denotes that this segment has an acknowledgement, and carrying the next sequence wanted from the destination of this segment.
PSH 1 Request for push. It allows the application to send the data even if the buffer is not full. When set to 1, indicates that the TCP pushes the data immediately.
RST 1 Reset the connection. If the sender encountered a problem with the TCP connection, it is used to terminate the connection. This can be sent from the receiver’s side when a packet is sent to a wrong address or host.
SYN 1 Synchronize sequence numbers. This segment is a request to synchronize sequence numbers and establish a connection; this bit contains the sequence number of the first byte sent.
FIN 1 Terminate the connection. This bit is used to request the termination of the connection. Applications use FIN bit to close the TCP connection in one direction to terminate the connection.

3.2 Data TCP Segments

The TCP segment consists of the TCP header that is referred to as "segment header", and the payload, data, or "segment data of various size".

The Picture 3-1 shows that a TCP segment which contains the real bytes of data being sent between devices and the data offset field indicates the start position of the data is offset from the beginning of the TCP segment simultaneously.

Example of a TCP segment carrying data and header


4. TCP Message Flow: Handshake and Termination

4.1. Establishing a Connection (TCP 3-way Handshaking)

TCP 3-way handshake to establish a connection

[STEP #1] The client sends a SYN packet to request a connection to the server. After sending the SYN packet, the client state changes to SYN-SENT to wait a SYN+ACK.

[STEP #2] When the server receives the SYN request, the server sends ACK with SYN flag, that has a purpose to confirm the SYN and wait an ACK of the client. The server state will be changed to SYN_RECEIVED at the last.

[STEP #3] After the client sends an ACK to the server, a connection is established between the client and the server and the data transmission is available. The both client and server state are ESTABLISHED.


4.2. Terminating a Connection

TCP connection termination using 4-step handshake


[STEP #1] The client sends a FIN packet to terminate the existing connection with the server.

[STEP #2] The server sends an ACK packet to client to confirm the FIN and waits for finishing some data exchange to the client under the TIME_WAIT state (server state).

[STEP #3] When finishing the data exchange to the client, the server sends FIN packet to client to notice the existing connection was closed.

[STEP #4] The client sends an ACK for the FIN of the step #3. What if some data packet of the server on the step #3 is delayed caused by a routing problem of packet missing? So, the data packet can be arrived later than FIN of step #3, the data packet that arrived after the connection is closed, will be dropped or missing. Client keeps the existing connection with TIME_WAIT (client state) for some period (default 240 seconds) against this case even if the client receives the FIN packet.

5. TCP Protocol Features: Reliable, Full-Duplex, and More

5.1. Connection-oriented protocol

TCP needs a logical connection to be established between the two peers before data is communicated. The connection must be preserved during the whole time that data transmission is occurring, then released afterwards.

The connection is established through a three-way handshake between client and server processes and it is released using a four-way handshake between them.

5.2. Reliability

The TCP must recover from data that is damaged, lost, duplicated, or delivered out of order by the internet communication system.

This is achieved by assigning a sequence number to each octet transmitted and requiring a positive acknowledgment (ACK) from the receiving TCP. If the ACK is not received within a timeout interval, the data is retransmitted.

5.3. Full duplex

TCP connection provides a full-duplex service. Full duplex means exchanging data (sending and receiving) between two entities at the same time; in other words, "If there is a TCP connection between Process A on one host and Process B on another host, then application layer data can flow from Process A to Process B at the same time as application layer data flows from Process B to Process A".

5.4. Point-to-point

A TCP connection is always point-to-point, that is, the connection includes a single sender and a single receiver.

6. References

Comments

Popular posts from this blog

Resolving Key Exchange Failure When Connecting with SecureCRT to OpenSSH

SecureCRT] How to Back Up and Restore SecureCRT Settings on Windows

How to Set Up Vaultwarden (Bitwarden) on Synology NAS (Best Free Alternative to LastPass)