Skip to content

Commit 73b3858

Browse files
authored
Merge pull request #678 from Vipullakum007/Python-networking-docs
python networking docs added
2 parents 95f3a24 + cd2c9a8 commit 73b3858

File tree

5 files changed

+656
-0
lines changed

5 files changed

+656
-0
lines changed

docs/python/Networking/_category.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Networking in Python",
3+
"position": 2,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "In this section, you will learn about Networking in Python. You will explore various libraries and methods to handle networking tasks such as socket programming, HTTP requests, and more."
7+
}
8+
}

docs/python/Networking/generics.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
id: generics
3+
title: Generics
4+
sidebar_label: Generics
5+
sidebar_position: 4
6+
tags: [python, generics, type hints, type safety, code flexibility]
7+
description: In this tutorial, you will learn about generics in Python. We will cover how to define functions, classes, or methods that can operate on multiple types while maintaining type safety using type hints and type variables.
8+
---
9+
10+
In Python, generics is a mechanism with which you to define functions, classes, or methods that can operate on multiple types while maintaining type safety. With the implementation of Generics enable it is possible to write reusable code that can be used with different data types. It ensures promoting code flexibility and type correctness.
11+
12+
Generics in Python are implemented using type hints. This feature was introduced in Python with version 3.5 onwards.
13+
14+
Normally, you don't need to declare a variable type. The type is determined dynamically by the value assigned to it. Python's interpreter doesn't perform type checks and hence it may raise runtime exceptions.
15+
16+
Python's new type hinting feature helps in prompting the user with the expected type of the parameters to be passed.
17+
18+
Type hints allow you to specify the expected types of variables, function arguments, and return values. Generics extend this capability by introducing type variables, which represent generic types that can be replaced with specific types when using the generic function or class.
19+
20+
## Example 1
21+
22+
Let us have a look at the following example that defines a generic function −
23+
24+
```python
25+
from typing import List, TypeVar
26+
27+
T = TypeVar('T')
28+
29+
def reverse(items: List[T]) -> List[T]:
30+
return items[::-1]
31+
```
32+
33+
Here, we define a generic function called 'reverse'. The function takes a list ('List[T]') as an argument and returns a list of the same type. The type variable 'T' represents the generic type, which will be replaced with a specific type when the function is used.
34+
35+
## Example 2
36+
37+
The function `reverse()` function is called with different data types −
38+
39+
```python
40+
numbers = [1, 2, 3, 4, 5]
41+
reversed_numbers = reverse(numbers)
42+
print(reversed_numbers)
43+
44+
fruits = ['apple', 'banana', 'cherry']
45+
reversed_fruits = reverse(fruits)
46+
print(reversed_fruits)
47+
```
48+
49+
It will produce the following output −
50+
51+
```
52+
[5, 4, 3, 2, 1]
53+
['cherry', 'banana', 'apple']
54+
```
55+
56+
## Example 3
57+
58+
The following example uses generics with a generic class −
59+
60+
```python
61+
from typing import TypeVar, Generic
62+
63+
T = TypeVar('T')
64+
65+
class Box(Generic[T]):
66+
def __init__(self, item: T):
67+
self.item = item
68+
69+
def get_item(self) -> T:
70+
return self.item
71+
```
72+
73+
Let us create objects of the above generic class with `int` and `str` type
74+
75+
```python
76+
box1 = Box(42)
77+
print(box1.get_item())
78+
79+
box2 = Box('Hello')
80+
print(box2.get_item())
81+
```
82+
83+
It will produce the following output −
84+
85+
```
86+
42
87+
Hello
88+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
id: networking-programming
3+
title: Network Programming
4+
sidebar_label: Network Programming
5+
sidebar_position: 1
6+
tags: [python, networking, socket programming, TCP, UDP, network protocols]
7+
description: In this tutorial, you will learn about network programming in Python. We will cover the basics of sockets, higher-level network protocols, and the corresponding Python modules for these protocols.
8+
---
9+
10+
# Python - Network Programming
11+
12+
The threading module in Python's standard library is capable of handling multiple threads and their interaction within a single process. Communication between two processes running on the same machine is handled by Unix domain sockets, whereas for the processes running on different machines connected with TCP (Transmission Control Protocol), Internet domain sockets are used.
13+
14+
![Image](https://www.tutorialspoint.com/python/images/network_programming.jpg)
15+
16+
Python's standard library consists of various built-in modules that support interprocess communication and networking. Python provides two levels of access to the network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols.
17+
18+
Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.
19+
20+
## Network Protocols and Corresponding Python Modules
21+
22+
| Protocol | Common Function | Port No | Python Module |
23+
|----------|-----------------|---------|---------------|
24+
| HTTP | Web pages | 80 | `httplib`, `urllib`, `xmlrpclib` |
25+
| NNTP | Usenet news | 119 | `nntplib` |
26+
| FTP | File transfers | 20 | `ftplib`, `urllib` |
27+
| SMTP | Sending email | 25 | `smtplib` |
28+
| POP3 | Fetching email | 110 | `poplib` |
29+
| IMAP4 | Fetching email | 143 | `imaplib` |
30+
| Telnet | Command lines | 23 | `telnetlib` |
31+
| Gopher | Document transfers | 70 | `gopherlib`, `urllib` |

0 commit comments

Comments
 (0)