EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Python String Tutorial Python Stream

Python Stream

Abhilasha Chougule
Article byAbhilasha Chougule
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated June 30, 2023

Python Stream

Introduction to Python stream

In this article, we explore streams in Python as async or await primitives, which are high-level constructs that facilitate working with network connections. In Python, streams are utilized in IO modules to facilitate different input/output operations. However, this article specifically focuses on Python streams that are used for sending and receiving information without relying on callbacks, protocols, or transport mechanisms. The article covers various stream functions and demonstrates their usage with the asyncio module for sending and receiving data.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Functions of Python Stream

In this section, we will discuss the stream functions. The stream functions are functions that are top-level asyncio functions to work with the stream, create streams, etc.

Now let us see stream functions in detail:

1. Open_connection()

The function asyncio.open_connection() is used as a coroutine and serves as a wrapper for establishing connections. It works in conjunction with the create_connection() function, which returns a pair containing instances of StreamReader and StreamWriter. This stream function takes parameters such as coroutine asyncio.open_connection( host, port, *, loop, limit, **kwds). These arguments are usually used for creating a connection with the create_connection() function

2. Start_server()

The function “asyncio.start_server()” is a stream function used to start a socket server in Python. It uses coroutine along with the asyncio module. The function is declared as “asyncio.start_server(client_connected_cb, host, port, loop, limit, …)”. The parameters of the “start_server()” function are as follows:

The parameter “client_connected_cb” specifies a coroutine function that automatically schedules tasks when a new client connects to the server. It handles the client’s requests and interactions.
The “host” parameter specifies the IP address or hostname on which the server will listen for incoming connections. It can listen on all available interfaces by setting it to an empty string or “None.”

What is Python Stream?

In this Python stream, we have seen we have instances like reader and writer of StreamReader and StreamWriter. We will see in detail these in the below section. This StreamReader makes itself a subclass of the protocol because it can potentially prevent it from calling inappropriate methods of the protocol.

The StreamWriter class in the Python asyncio stream is declared as “class asyncio.StreamWriter(transport, protocol, reader, loop)”. It provides various functions for interacting with the transport:

The write() function is used to write data to the transport.
The writelines() function is used to write a list of lines to the transport without encountering newline characters.
The get_extra_info() function returns optional extra information about the transport.
The write_eof() function closes the write end of the transport after flushing all the buffered data.
The can_write_eof() function checks if the transport supports write_eof and returns true or false accordingly.
The close() function is used to close the transport.

Conclusion

In this article, we also saw stream classes such as StreamReader and StreamWriter, which provide instances like reader and writer used in stream functions. These stream classes provide different functions for reading and writing the information to the transport or buffer.

Recommended Articles

This is a guide to Python Stream. Here we discuss an introduction to Python Stream and the stream functions with detailed explanation. You can also go through our other related articles to learn more –

  1. Python Pickle
  2. Python Generators
  3. Python Map Function
  4. Tokenization in Python
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
PYTHON Course Bundle - 81 Courses in 1 | 59 Mock Tests
 257+ Hours of HD Videos
81 Courses
59 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.8
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more