PYTHON-
NETWORK PROGRAMMING
What is sockets ?
Sockets are the endpoints of a
bidirectional communications channel . Sockets may communicate within a
process , between processes on the same machine, or between processes on
different continents.
http - Not secure
https - Secure
S.no |
Term &
description |
1 |
DOMAIN The family of protocols that is used
as the transport mechanism. these values are constants such as A S_INET_,
PF_INET, PF_X25, and so on. |
2 |
TYPE The type of communications between the
two endpoints, typically SOCK_STREAM
for connection-oriented protocols and SOCK_DGRAM for connectionless protocols. |
3 |
PROTOCOL Typically zero, this may be used to
identify a variant of a protocol within a domain and type . |
4 |
HOSTNAME The identifier of a network
interface ? ·
A string, which can be a host name, a dotted-quad address, or an
IPV6 address in colon(and possibly dot)notation |
|
·
A string "<broadcast>", which specifies
an INADDR_BROADCAST address ·
A zero-length string,,
which specifies INADDR_ANY,or ·
An integer, interpreted as
a binary address in host byte order |
5 |
PORT Each server listens for clients
calling on one more ports. a port may be a fixnum port number, a string
containing a port number, or the name of a service |
SERVER
SOCKET METHODS
S.no |
Method &
description |
1 |
s.bind() this method
binds address(hostname, port number pair ) to socket |
2 |
s.listen() this method
sets up an start TCP listener |
3 |
s.accept() This
passively accept TCP client connection, waiting until connection arrives(blocking) |
CLIENT SOCKETS METHODS
S.no |
Method &
description |
1 |
s.connect() this method
actively initiates TCP server connection. |
Generel socket Methods
S.No |
Method &
Description |
1 |
s.recv() This method
recives TCP messege |
2 |
s.send() This method
transmits TCP messege |
3 |
s.recvfrom() This method
recives UDP message |
4 |
s.sendto() This method
transmits UDP messege |
5 |
s.close
() This method
closes socket |
6 |
Socket.gethostname
() Return the
hostname |
A Simple Server
#
This is server.py file
import socket
s=socket.socket()
host=socket.gethostname()
port=12345
s.bind((host,port))
s.listen(5)
while True:
c,addr=s.accept()
print('Got connection from',addr)
c.send('Thank you for connecting')
c.close()
A Simple Client
# This is client.py file
import socket
s =
socket.socket()
host =
socket.gethostname()
port = 12345
s.connect((host,port))
print(s.recv(1024))
s.close
#following
would start a server in backgroud .
$
python server.py &
#
once server is started run client as follows:
$
python client.py
Output
got
connection from ('127.0.0.1',48437)
thank
you for connecting
0 comments:
Post a Comment