2017.07.07 Python network programming print device name and IPV4 address

Source: Internet
Author: User
Tags network function string format

1. Easy to understand directly on the code:

#-*-coding=gb2312-*-
#! /usr/bin/env python
# Python Network Programming Cookbook--chapter-1
# This program was optimized for Python 2.7. It may run on any
# other Python version with/without modifications
#-*-coding=gb2312-*-
Import Socket

Def print_machine_info ():
Host_name=socket.gethostname ()
Ip_address=socket.gethostbyname (HOST_NAME)

Print "Host name is:%s"% host_name
Print "IP address is:%s"% ip_address


If __name__== ' __main__ ':
Print_machine_info ()

2. Obtain the IP address of the remote device:

#-*-Coding:utf-8-*-
#!/usr/bin/env/python

# Python Network Programming Cookbook--chapter-1
# Optimized for Python 2.7
# It may run on any other version with/without modifications

Import socket

Def get_remote_machine_info ():
Remote_host = ' www.hqu.edu.cn '
Try
Print "Host IP address is:%s"%socket.gethostbyname (Remote_host)
Except socket.error,err_msg:
Print "%s:%s"% (remote_host,err_msg)

If __name__== ' __main__ ':
Get_remote_machine_info ()

3. Convert the IPV4 address to a different format:

#-*-Coding:utf-8-*-
# If you want to use the underlying network function, sometimes the ordinary string format IP address is not very useful, we need to convert it to a packaged 32-bit binary format

#!/usr/bin/env Python
# Python Network Programming Cookbook--chapter-1
# Optimized for Python 2.7
# It may run on any other version with/without modifications

Import socket
From BINASCII import hexlify

Def convert_ipv4_address ():
For ip_addr in [' 127.0.0.1 ', ' 192.168.0.1 ']:
Packed_ip_addr =socket.inet_aton (IP_ADDR)
UNPACKED_IP_ADDR = Socket.inet_ntoa (packed_ip_addr)

Print "IP Address:%s = After Packaging:%s, before packaging:%s" \
% (Ip_addr,hexlify (packed_ip_addr), unpacked_ip_addr)


If __name__== ' __main__ ':
Convert_ipv4_address ()

4. Locate the service name via the specified port and protocol:

#-*-Coding:utf-8-*-
# If you want to find a network service, it's best to know which port the service is running on TCP or UDP protocol
#!usr/bin/env Python
# Python Network Programming Cookbook--chapter-1
# Optimized for Python 2.7
# It may run on any other version with/without modifications

Import socket
Def find_service_name ():
ProtocolName = ' TCP '
For port in [80,25]:
Print "Port:%s + = Service Name is:%s "% (Port,socket.getservbyport (port,protocolname)) \

Print "Port:%s + = Service Name is:%s"% (53,socket.getservbyport (+, ' UDP '))

If __name__== ' __main__ ':
Find_service_name ()

5. Convert between host byte order and network byte:

#-*-Coding:utf-8-*-
# when writing the underlying network application, it may be necessary to deal with the underlying data transmitted between the two devices via a cable, and the data emitted from the host operating system needs to be transformed into a network of various
#!usr/bin/env Python
# Python Network Programming Cookbook--chapter-1
# Optimized for Python 2.7
# It may run on any other version with/without modifications

Import socket

Def convert_integer ():
data=1234
print "source byte:%s = = Long host byte order:%s,network byte order:%s" \
% (Data,socket.ntohl (data), SOCKET.HTONL (data))

print "Source byte:%s" = Short host byte order:%s,network byte order:%s "\
% (Data,socket.ntohs (data), socket.htons (data))

If __name__== ' __main__ ':
Convert_integer ()

2017.07.07 Python network programming print device name and IPV4 address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.