structure updates
This commit is contained in:
92
tech_docs/python/socket.md
Normal file
92
tech_docs/python/socket.md
Normal file
@@ -0,0 +1,92 @@
|
||||
In the realm of network programming and socket management in Python on Linux systems, the `socket` library stands out as a fundamental and powerful tool. The `socket` library in Python provides access to the BSD socket interface, offering a low-level means to create client/server programs, manage connections, send and receive data over the network, and interact with various network protocols directly from Python scripts. This library is particularly valuable for developing network applications, implementing custom protocols, or creating tools for network testing and exploration.
|
||||
|
||||
### Socket Library Reference Guide
|
||||
|
||||
#### Installation
|
||||
The `socket` library is part of Python's standard library, so it's available in any standard Python installation without the need for additional packages.
|
||||
|
||||
#### Basic Usage
|
||||
|
||||
##### Creating a Socket
|
||||
To create a socket, you simply need to import the `socket` module and use the `socket()` function, specifying the address family and socket type.
|
||||
|
||||
```python
|
||||
import socket
|
||||
|
||||
# Create an IPv4 (AF_INET) socket object using the TCP protocol (SOCK_STREAM)
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
```
|
||||
|
||||
##### Connecting to a Server
|
||||
For a client program, use the `connect()` method to establish a connection to a server.
|
||||
|
||||
```python
|
||||
server_address = ('hostname', 10000) # Hostname and port number
|
||||
sock.connect(server_address)
|
||||
```
|
||||
|
||||
##### Sending and Receiving Data
|
||||
Once a connection is established, data can be sent using `sendall()` and received with `recv()`.
|
||||
|
||||
```python
|
||||
# Sending data
|
||||
message = 'This is the message. It will be repeated.'
|
||||
sock.sendall(message.encode('utf-8'))
|
||||
|
||||
# Receiving data
|
||||
data = sock.recv(1024)
|
||||
print('Received', data.decode('utf-8'))
|
||||
```
|
||||
|
||||
##### Closing a Socket
|
||||
After communication is complete, close the connection to free up resources.
|
||||
|
||||
```python
|
||||
sock.close()
|
||||
```
|
||||
|
||||
##### Creating a Server
|
||||
To create a server that listens for incoming connections, bind the socket to an address and port, and call `listen()` to start accepting connections.
|
||||
|
||||
```python
|
||||
# Create a TCP/IP socket
|
||||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# Bind the socket to the port
|
||||
server_address = ('localhost', 10000)
|
||||
server_socket.bind(server_address)
|
||||
|
||||
# Listen for incoming connections
|
||||
server_socket.listen()
|
||||
|
||||
while True:
|
||||
# Wait for a connection
|
||||
connection, client_address = server_socket.accept()
|
||||
try:
|
||||
# Receive the data in small chunks and retransmit it
|
||||
while True:
|
||||
data = connection.recv(16)
|
||||
if data:
|
||||
connection.sendall(data)
|
||||
else:
|
||||
break
|
||||
finally:
|
||||
# Clean up the connection
|
||||
connection.close()
|
||||
```
|
||||
|
||||
#### Use Cases
|
||||
- **Custom Network Protocols**: Implementing specialized communication protocols for unique application requirements.
|
||||
- **Network Utilities**: Building tools for network diagnostics, such as port scanners, ping implementations, or simple HTTP servers.
|
||||
- **Socket-based IPC**: Utilizing sockets for inter-process communication on a single system, leveraging UNIX domain sockets (`AF_UNIX`).
|
||||
|
||||
#### Integration with Linux Systems
|
||||
Python's `socket` library is particularly powerful on Linux due to the operating system's robust networking stack and support for various address families and socket types, including UNIX domain sockets for IPC.
|
||||
|
||||
#### Security Considerations
|
||||
When developing network applications:
|
||||
- Implement proper error handling to manage exceptions, such as connection timeouts or refused connections.
|
||||
- Consider encryption for data in transit to protect sensitive information.
|
||||
- Validate all data received over the network to prevent injection attacks or buffer overflows.
|
||||
|
||||
The `socket` library is a cornerstone for network programming in Python, offering deep integration with Linux's networking capabilities. It allows developers to create sophisticated network applications and utilities, harnessing the full power of the system's networking capabilities directly from Python.
|
||||
Reference in New Issue
Block a user