Python obtains the current public ip address and automatically disconnects the broadband connection instance code. python instance

Source: Internet
Author: User

Python obtains the current public ip address and automatically disconnects the broadband connection instance code. python instance

Today, I wrote a file to get the current public ip address and automatically disconnect the broadband connection.

The specific purpose of this file is understandable. It can be used, but it is currently only applicable to Windows. My Python version is 2.7, And the win32ras module needs to download pywin32.

The Code is as follows:

#! Coding: cp936 import win32ras import time, OS def Connect (dialname, account, passwd): dial_params = (dialname, '','', account, passwd, '') return win32ras. dial (None, None, dial_params, None) def DialBroadband (): dialname = 'broadband connection' # just a name account = '********* 'passwd = '*************** * 'try: # handle is a pid, for disconnect or showipadrress, if connect success return 0. # account is the username That your ISP supposed, passwd is the password. handle, result = Connect (dialname, account, passwd) if result = 0: print "Connection success! "Return handle, result else: print" Connection failed, wait for 5 seconds and try again... "time. sleep (5) DialBroadband () failed t: print "Can't finish this connection, please check out. "return def Disconnect (handle): if handle! = None: try: win32ras. HangUp (handle) print "Disconnection success! "Return" success "failed T: print" Disconnection failed, wait for 5 seconds and try again... "time. sleep (5) Disconnect () else: print "Can't find the process! "Return def Check_for_Broadband (): connections = [] connections = win32ras. enumConnections () if (len (connections) = 0): print "The system is not running any broadband connection. "return else: print" The system is running % d broadband connection. "% len (connections) return connections def ShowIpAddress (handle): print win32ras. getConnectStatus (handle) data = OS. popen ("ipconfig", "r "). readlines () hav E_ppp = 0 ip_str = None for line in data: if line. find ("Broadband connection")> = 0: have_ppp = 1 # if your system language is English, you shoshould write like this: # if have_ppp and line. strip (). startswith ("IP Address"): # in othewords, replace the "IPv4 Address" to "IP Address" if have_ppp and line. strip (). startswith ("IPv4 address"): ip_str = line. split (":") [1]. strip () have_ppp = 0 print ip_str # get my ipaddress anf disconnect Broadband connection. def main (): data = Check_for_Broadband () # if exist running broadband connection, disconnected it. if data! = None: for p in data: ShowIpAddress (p [0]) if (Disconnect (p [0]) = "success"): print "% s has been disconnected. "% p [1] time. sleep (3) else: pid, res = DialBroadband () ShowIpAddress (pid) time. sleep (3) Disconnect (pid) return "finsh test" test = main () print test

Basic annotations are provided for your reference.

Summary

The above is all about Python's code for getting the current public ip address and automatically disconnecting the broadband connection instance. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.