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