Skip to content

python networking docs added #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/python/Networking/_category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Networking in Python",
"position": 2,
"link": {
"type": "generated-index",
"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."
}
}
88 changes: 88 additions & 0 deletions docs/python/Networking/generics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
id: generics
title: Generics
sidebar_label: Generics
sidebar_position: 4
tags: [python, generics, type hints, type safety, code flexibility]
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.
---

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.

Generics in Python are implemented using type hints. This feature was introduced in Python with version 3.5 onwards.

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.

Python's new type hinting feature helps in prompting the user with the expected type of the parameters to be passed.

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.

## Example 1

Let us have a look at the following example that defines a generic function −

```python
from typing import List, TypeVar

T = TypeVar('T')

def reverse(items: List[T]) -> List[T]:
return items[::-1]
```

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.

## Example 2

The function `reverse()` function is called with different data types −

```python
numbers = [1, 2, 3, 4, 5]
reversed_numbers = reverse(numbers)
print(reversed_numbers)

fruits = ['apple', 'banana', 'cherry']
reversed_fruits = reverse(fruits)
print(reversed_fruits)
```

It will produce the following output −

```
[5, 4, 3, 2, 1]
['cherry', 'banana', 'apple']
```

## Example 3

The following example uses generics with a generic class −

```python
from typing import TypeVar, Generic

T = TypeVar('T')

class Box(Generic[T]):
def __init__(self, item: T):
self.item = item

def get_item(self) -> T:
return self.item
```

Let us create objects of the above generic class with `int` and `str` type

```python
box1 = Box(42)
print(box1.get_item())

box2 = Box('Hello')
print(box2.get_item())
```

It will produce the following output −

```
42
Hello
```
31 changes: 31 additions & 0 deletions docs/python/Networking/networking-programming.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
id: networking-programming
title: Network Programming
sidebar_label: Network Programming
sidebar_position: 1
tags: [python, networking, socket programming, TCP, UDP, network protocols]
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.
---

# Python - Network Programming

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.

![Image](https://www.tutorialspoint.com/python/images/network_programming.jpg)

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.

Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.

## Network Protocols and Corresponding Python Modules

| Protocol | Common Function | Port No | Python Module |
|----------|-----------------|---------|---------------|
| HTTP | Web pages | 80 | `httplib`, `urllib`, `xmlrpclib` |
| NNTP | Usenet news | 119 | `nntplib` |
| FTP | File transfers | 20 | `ftplib`, `urllib` |
| SMTP | Sending email | 25 | `smtplib` |
| POP3 | Fetching email | 110 | `poplib` |
| IMAP4 | Fetching email | 143 | `imaplib` |
| Telnet | Command lines | 23 | `telnetlib` |
| Gopher | Document transfers | 70 | `gopherlib`, `urllib` |
Loading
Loading