EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Python String Tutorial Python Stream
 

Python Stream

Abhilasha Chougule
Article byAbhilasha Chougule
EDUCBA
Reviewed byRavi Rathore

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW