- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- Python Versions
- Python Environment Setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control & Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break Vs continue
- Python pass Vs continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List Vs Tuple Vs Dict Vs Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python CGI Programming
- Python Network Programming
- Python Send E-mail
- Python Multi-threading
- Python XML Processing
- Python MySQL Database
- Python GUI Programming
- Python Event Handling
- Python Keywords
- Python All Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
- Python Test
- Python Online Test
- Give Online Test
- All Test List
Python Networking
Python allows you to two levels of access to the network services:
- at low level - in the low level, you can only access to the basic socket support in the underlying OS, allows you to implement the clients and servers for both connection-oriented and connectionless protocols
- at high level - python have libraries, provides higher-level access to the particular application-level network protocols, such as HTTP, FTP, etc.
What is Socket ?
Socket is the endpoint of a bi-directional communications channel. Sockets can communicate within a process, between the processes on the same machine, or between the processes on the different continents.
A Simple Server
To write the internet server, we will use the function socket() available in the socket module to create the socket object. A socket object is then used to call the other functions to setup the socket server. Now call the function named bind(hostname, port) to specify a port for your service on the given host. Next, call the method named accept() of the returned object. This method waits until a client connects to the port you have specified, and then returns a connection object that represents the connection to that client. Here is an example demonstrating all this. This is server.py file:
# Python Networking - Example Program import socket # this import the socket module s = socket.socket() # this creates a socket object host = socket.gethostname() # this will get the local machine name port = 12345 # this reserves a port for your service. s.bind((host, port)) # this bind to the port s.listen(5) # this wait for the client connection. while True: c, addr = s.accept() # this establish connection with the client. print("Got connection from", addr) c.send("Thank you for connecting") c.close() # this will lose the connection
A Simple Client
Here is a simple client program, which opens a connection to a given port 12345 and given host. This is client.py file.
# Python Networking - Example Program import socket s = socket.socket() host = socket.gethostname() port = 12345 s.connect((host, port)) print(s.recv(1024)) s.close()
Now run the server.py file in the background and then run the client.py to see the result:
$ python server.py & $ python client.py
It will produce following result:
Got connection from ('127.0.0.1', 48437) Thank you for connecting
« Previous Tutorial Next Tutorial »