Click here to load reader
Upload
learnbayin
View
64
Download
2
Embed Size (px)
Citation preview
Python Network Basics And Socket Programming
Socket programming in Python
TCP Layers
.
Layers Explained
Application Layer: Top most Layer and is is utilized to ensure communication
between applications on a network.example:DNS,HTTP,FTP,SSH,DHCP etc
Transport Layer: Transport layer ensures whole message arrives intact and in
order, ensuring both error control and flow control at the source to destination
level.example:
TCP,UDP,RDP
Internet Layer:IP works at this layer.this protocol has the responsibility of
identifying hosts based upon their logical addresses and to route data among
them over the underlying network.example- IPV4,IPV6
Layers...
Network Access Layer:The Network Access Layer is the layer in the TCP/IP model
at which data is transmitted and received across the physical
network.Example:Ethernet,ATM
How TCP Works:
1. SYN: open is performed by the client sending a SYN to the server. The client sets the segment's sequence number to a
random value A.
2. SYN-ACK: In response, the server replies with a SYN-ACK. The acknowledgment number is set to one more than the
received sequence number i.e. A+1, and the sequence number that the server chooses for the packet is another random
number, B.
3. ACK: Finally, the client sends an ACK back to the server. The sequence number is set to the received acknowledgement
value i.e. A+1, and the acknowledgement number is set to one more than the received sequence number i.e. B+1.
Sockets
Sockets may communicate within a process, between processes on the same
machine, or between processes on different continents.
Socket Module:
s = socket.socket (socket_family, socket_type, protocol=0)
socket_family: This is either AF_UNIX or AF_INET(For IPV4),AF_INET6 (For IPV6)
Socket Type: This is either SOCK_STREAM(For TCP) or SOCK_DGRAM(UDP)
Sockets
Basic Example Code:
Create a Socket :
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Get IP Address by hostname:
ip = socket.gethostbyname('www.google.com')
print ip
Sockets..
s.bind() This method binds address (hostname, port number pair) to
socket,,otherwise it doesn't know what address (ip-address/port pair) it should
listen to.
s.bind((HOST, PORT))
s.listen(arg) : This method sets up and start TCP listener.the argument to listen
tells the socket library that we want it to queue up as many as 5 connect requests
(the normal max) before refusing outside connections.
s.listen(5)
sockets...
s.accept() : accept TCP client connection, waiting until connection arrives.
accept() returns an open connection between the server and client, along with the
address of the client.
connection, client_address = sock.accept()
select.select(): It monitors sockets, open files, and pipes (anything with a fileno()
method that returns a valid file descriptor) until they become readable or writable.
makes it easier to monitor multiple connections at the same time.
readable, writable, exceptional = select.select(inputs, outputs, inputs) /*Wait for at least one of the sockets to be ready for
processing.
Code for sockets
BAsic Socket Code: https://repl.it/HKOF/0
The above Code create a socket,resolves the ip and then connect to the ip/host.
Chat Server And Client
Chat Server: