To improve blogger's written  English level,this post will be written in English.

Introduction

This blog records a simple Python web network code,it's mainly about information search included like subdomain, whois information and a web's server IP (briefly).

Python basic  syntax

The sys module

 To use the command line like "python program.py -u xxx " ,we will us the module sys

Let's use a simple program to show that.

import sys

def tip():
    tip = """
    Please use this program like the correct form:
           python program.py -u url key
    """
    print(tip)

def main():
    if (len(sys.argv)==4):
        url = sys.argv[1]
        key = sys.argv[3]
        if(sys.argv[2]=='-u'):
            print(f'The value are url:{url},key:{key}')
        else:
            tip()

    else:
        tip()

if __name__ == "__main__":
    main()

We can see this code  extracts the values that we input and prints them.

So we can use it like this

python blog_program.py  -u csdn.net 123

Its output

The value are url:csdn.net,key:123

But if we input like this

python blog_program.py  -u csdn.net

We can clearly see that this shell code lacks the value of key, so the last output is this


    Please use this program like the correct form:
           python program.py -u url key

The  whois module

A website's WHOIS information consists ofT  the registration date and expiration date of this website's URL.

Like the previous example,we also use a piece of code to explain this Library.

First, install the library: pip install python-whois

from whois import whois

url = 'https://www.csdn.net'
data = whois(url)
print(data)

And if we print the type of whois ,we will see <class 'function'>, so this whois actually is a function.

A simple eg.

I show a program used the modules that i tought.

The source code:

import socket
from whois import whois
import sys

def ping (url):
    ip = socket.gethostbyname(url)
    sum=f"""
    ---------------------------------------
          url           {url}        
          ip            {ip}     
    ---------------------------------------
    """
    print(sum)

def whoisurl(url):
    data = whois(url)
    print(data)

def main():
    if len(sys.argv) == 3:
        url = sys.argv[1]
        if sys.argv[2]=='1':
            ping(url)
        elif sys.argv[2]=='2':
            whoisurl(url)

        else:
            print("格式错误!")
    else:
        tip = """
        请按照如下格式输入:
        查询目标url ip: python pingwhois.py url 1
        whois 查询:    python pingwhois.py url 2 
        """
        print(tip)


if __name__ == '__main__':
    main()

Okay,the program achieve two main functions

the command line like can search a website's IP.

python pingwhois.py url 1

Actually,this IP is only  protected by CDN,but we only do a simple example.

Ok,the second like this:

python pingwhois.py  www.csdn.net 2

Summary

This post introducts how to use these modules linked python web network code like sys and whois.

In the end this post  shows a simple program.

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐