Networking in Python, Sockets
The key to networking in Python is the socket module, which provides a library for making network connections using Python.
The socket module is used for low level networking and it provides the standard BSD Sockets API. Unix programmers are very familiar with this networking service. If you want to build server sockets and clients, this the module that everything for you. There are a number of channels in which a socket may be implemented, such as TCP, UDP, Unix sockets and so on. Any communication goes through sockets, once you create a socket and are successfully connected you can read and write information from it. When you connect to facebook.com, your computer creates a client socket which will fetch the Facebook page. In simple words, the client socket retrieves information from the Facebook servers. To create a socket you must first import the socket module and use the socket.socket() function to create it. The syntax for this is shown below:
>>>import socket >>>sock=socket.socket(socket_family,socket_type,protocol=0)
The socket family is either AF_UNIX or AF_INET. The socket type is either SOCK_STREAM or SOCK_DGRAM and the protocol default is set to zero. Once you have created the socket object you can use it to create a client socket or server socket by using specific functions for each one.
Server Socket Methods
s.bind() This method binds address (hostname, port number pair) to socket. s.listen() This method sets up and start TCP listener. s.accept() This passively accept TCP client connection, waiting until connection arrives (blocking).
Client Socket Methods
s.connect() This method actively initiates TCP server connection.
After you have learned server socker methods and client socket methods you can create a simple client which connect to a simple server. You can use your imagination to create a simple chat. You can read my previous articles on how to create a server and client in Python programming language.
Like us on Facebook
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint : Tor, The Onion Router, is a network of Virtual Tunnels that allows users to communicate securely and...1 comment |
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6 : Integria IMS is a fully featured ITIL management system, featuring a ticketing system, inventory/C...2 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7 : OpenLDAP is a free open source Light Weight Directory Access protocol developed by the OpenLDAP proj...0 comments |
- Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comments |
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6
- Install LibreOffice 4.1.3 in Elementary OS ‘Luna’
- How To Install Simple Scan in Crunchbang ‘Waldorf’
- Selene Media Encoder: Convert Audio, Video Files To Most Popular Formats
- Linux Kernel 3.12.2 Is Available For Download! Install / Upgrade Instructions
- CentOS 6.5 Has Been Officially Released!
- Format Junkie: Convert Media Files To All Popular Formats
- Setup Squid Proxy Server On openSUSE 13.1
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .