SlideShare a Scribd company logo
1 of 12
Network Programming-Python
Get hostname of your system
import socket
Hostname=socket.gethostname()
print (“The hostnme of this computer is
%s”, Hostname)
socket: imports one of Python's core
networking libraries.
gethostname(): return the current host
name.
The hostnme of
this computer is
SITE11995
Network Programming-Python
c, addr = s.accept() //c-new socket to carry out
the communication , addr-client address.
c.send(1024 bytes data)
c.recv(1024 bytes data)
Socket
import socket // importing the module to support to make
connection
s=socket.socket( socket.AF_INET, socket.SOCK_STREAM)
// create a socket
//socket.SOCK_DGRAM: connectionless protocols
s.bind(host, port)
host=socket.gethostname()
s.listen(5) //server is listening for accepting client request.
5- Maximum Queue length to store the client connection
requests
Network Programming-Python
Get hostname of your system
import socket
Hostname=socket.gethostname( )
IPaddress=socket.gethostbyname(H
ostname)
print (“The IP Address of this
computer is %s”, IPaddress)
Gethostbyname():
Return the IP address (a string of the
form '255.255.255.255') for a
host.
IP Address of this
computer is:
172.16.17.146
Retrieving a remote machine's IP address
import socket
hostname=input ("enter remote machine information")
try:
Remote_IP=socket.gethostbyname(hostname)
print("%s IPAddress is:%s" %(hostname,Remote_IP))
except socket.error as err :
print("%s is ivalid %s" %(hostname,err))
output:
enter remote machine information=www.google.com
www.google.com IPAddress is:216.58.197.68
enter remote machine information=www.go
www.go is ivalid [Errno 11001] getaddrinfo failed
Converting an IPv4 address to different
formats
import socket
from binascii import hexlify
IPaddress=socket.gethostbyname("www.google.com")
packed_IPaddress=socket.inet_aton(IPaddress)
unpacked_IPaddress=socket.inet_ntoa(packed_IPaddress)
print("%s,hexadecimal representation %s: %s"
%(IPaddress,hexlify(packed_IPaddress),unpacked_IPaddress))
Output:
216.58.197.68,hexadecimal representation b'd83ac544':
216.58.197.68
Get service name of a port and protocol
import socket
port=80
protocol="tcp"
print("%s" %(socket.getservbyport(port,protocol)))
port=53
protocol="udp"
print("%s" %(socket.getservbyport(port,protocol)))
• Port: 80 => service name: http
• Port: 25 => service name: smtp
• Port: 53 => service name: domain
Set and Get Socket timeout
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_ST
REAM)
print(s.gettimeout())
s.settimeout(100)
print(s.gettimeout())
TCP/IP IN PYTHON-Server Program
import socket # Import socket module
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create a socket
object
host = socket.gethostname() # Get local machine name
port = 12348 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
c, addr = s.accept()
# Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
msg=" "
while msg !="bye":
msg=c.recv(1024)
print "client msg" ,msg
msg=raw_input("server>>>")
c.send(msg)
TCP/IP IN PYTHON-Client Program
import socket # Import socket module
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create a
socket object
host = socket.gethostname() # Get local machine name
port = 12348 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
msg=" "
while(msg!="bye"):
msg=raw_input("client>>>")
s.send(msg)
msg=s.recv(1024)
print"from server" +msg
s.close # Close the socket when done
File Transfer-Server
import socket
ser=socket.socket()
host=socket.gethostname()
port=1234
ser.bind((host,port))
ser.listen(5)
l=""
c,addr=ser.accept()
print(addr)
#while l!=bye:
f=open("server1.py","r")
l=f.read(1024)
while(l):
c.send(l)
l=f.read(1024)
c.close()
ser.close()
import socket
c=socket.socket()
host=socket.gethostname()
port=1234
c.connect((host,port))
x=c.recv(1024)
f=open("xxx.txt","w")
while(x):
print(x)
f.write(x)
x=c.recv(1024)
c.close()
File Transfer-Client

More Related Content

Similar to session6-Network Programming.pptx

Similar to session6-Network Programming.pptx (20)

Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Arduino and the real time web
Arduino and the real time webArduino and the real time web
Arduino and the real time web
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Os 2
Os 2Os 2
Os 2
 
Java socket presentation
Java socket presentation Java socket presentation
Java socket presentation
 
Network
NetworkNetwork
Network
 
EN-04 (1).pptx
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptx
 
Networks lab
Networks labNetworks lab
Networks lab
 
Networks lab
Networks labNetworks lab
Networks lab
 
java sockets
 java sockets java sockets
java sockets
 
Sockets
Sockets Sockets
Sockets
 
Networking lab
Networking labNetworking lab
Networking lab
 
nw-lab_dns-server.pdf
nw-lab_dns-server.pdfnw-lab_dns-server.pdf
nw-lab_dns-server.pdf
 
#2 (UDP)
#2 (UDP)#2 (UDP)
#2 (UDP)
 
Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
 
Socket.io v.0.8.3
Socket.io v.0.8.3Socket.io v.0.8.3
Socket.io v.0.8.3
 
Socket.io v.0.8.3
Socket.io v.0.8.3Socket.io v.0.8.3
Socket.io v.0.8.3
 

Recently uploaded

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 

Recently uploaded (20)

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 

session6-Network Programming.pptx

  • 1. Network Programming-Python Get hostname of your system import socket Hostname=socket.gethostname() print (“The hostnme of this computer is %s”, Hostname) socket: imports one of Python's core networking libraries. gethostname(): return the current host name. The hostnme of this computer is SITE11995
  • 2. Network Programming-Python c, addr = s.accept() //c-new socket to carry out the communication , addr-client address. c.send(1024 bytes data) c.recv(1024 bytes data)
  • 3. Socket import socket // importing the module to support to make connection s=socket.socket( socket.AF_INET, socket.SOCK_STREAM) // create a socket //socket.SOCK_DGRAM: connectionless protocols s.bind(host, port) host=socket.gethostname() s.listen(5) //server is listening for accepting client request. 5- Maximum Queue length to store the client connection requests
  • 4. Network Programming-Python Get hostname of your system import socket Hostname=socket.gethostname( ) IPaddress=socket.gethostbyname(H ostname) print (“The IP Address of this computer is %s”, IPaddress) Gethostbyname(): Return the IP address (a string of the form '255.255.255.255') for a host. IP Address of this computer is: 172.16.17.146
  • 5. Retrieving a remote machine's IP address import socket hostname=input ("enter remote machine information") try: Remote_IP=socket.gethostbyname(hostname) print("%s IPAddress is:%s" %(hostname,Remote_IP)) except socket.error as err : print("%s is ivalid %s" %(hostname,err)) output: enter remote machine information=www.google.com www.google.com IPAddress is:216.58.197.68 enter remote machine information=www.go www.go is ivalid [Errno 11001] getaddrinfo failed
  • 6. Converting an IPv4 address to different formats import socket from binascii import hexlify IPaddress=socket.gethostbyname("www.google.com") packed_IPaddress=socket.inet_aton(IPaddress) unpacked_IPaddress=socket.inet_ntoa(packed_IPaddress) print("%s,hexadecimal representation %s: %s" %(IPaddress,hexlify(packed_IPaddress),unpacked_IPaddress)) Output: 216.58.197.68,hexadecimal representation b'd83ac544': 216.58.197.68
  • 7. Get service name of a port and protocol import socket port=80 protocol="tcp" print("%s" %(socket.getservbyport(port,protocol))) port=53 protocol="udp" print("%s" %(socket.getservbyport(port,protocol))) • Port: 80 => service name: http • Port: 25 => service name: smtp • Port: 53 => service name: domain
  • 8. Set and Get Socket timeout import socket s=socket.socket(socket.AF_INET,socket.SOCK_ST REAM) print(s.gettimeout()) s.settimeout(100) print(s.gettimeout())
  • 9. TCP/IP IN PYTHON-Server Program import socket # Import socket module s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create a socket object host = socket.gethostname() # Get local machine name port = 12348 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. c, addr = s.accept() # Establish connection with client. print 'Got connection from', addr c.send('Thank you for connecting') msg=" " while msg !="bye": msg=c.recv(1024) print "client msg" ,msg msg=raw_input("server>>>") c.send(msg)
  • 10. TCP/IP IN PYTHON-Client Program import socket # Import socket module s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create a socket object host = socket.gethostname() # Get local machine name port = 12348 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) msg=" " while(msg!="bye"): msg=raw_input("client>>>") s.send(msg) msg=s.recv(1024) print"from server" +msg s.close # Close the socket when done