asynchat是asyncore的增强版。asyncore则是异步套接字处理程序。
2、Cookie、cookielib
Cookie对象操作,主要用于服务器。cookielib客户端的cookie的支持。
3、email
E-mail邮件消息的支持。包括MIME
4、imaplib
IMAP4客户端模块
5、mailbox
读取多种邮箱的格式
6、mailcap
通过mailcap文件访问MIME配置
7、mhlib
访问MH邮箱
8、poplib
POP客户端模块
9、robotparser
支持解析Web服务器的robot文件
10、SimpleXMLRPCServer
一个简单的XML-RPC服务器
11、smtpd、smtplib
SMTP服务器端模块、SMTP客户端模块
python标准库中常用的网络相关模块并不止以上这些。还有很多,但并不一定都需要了解,只需在需要使用的时候查找参考使用即可。
你说的总体是可行的,路由器都会提供一个web 的访问控制界面,python urllib2 可以利用POST 的方式登录,加一个header 就可以了,下面是登录一个BBS 的代码的header:
req=urllib.request.Request(url)req.add_header('User-agent','Mozilla/5.0')
req.add_header('Content-Type','application/x-www-form-urlencoded')
params=urllib.parse.urlencode({'id':username,
'login.x':24,
'login.y':8,
'login':'登陆',
'pw':password,
'titletype':'forum'
})
params=params.encode('gb2312')
print('Send request! waiting respose.....')
res=urllib2.urlopen(req,params)
下面是我ppp登录路由器的方式:
#!/usr/bin/python#
# License: GNU GPL v2
#
# Script for comtrend hg536+ ( firmware A101-302JAZ-C03_R21.A2pB021g.d15h )
# This scripts connect to router and reset ip in 45s. In this example use default values for jazztel .
import getpass
import sys
import telnetlib
import time
HOST = "192.168.1.1"
LOGIN = "admin"
PASS = "admin"
PROMPT = " -> "
WAITTIME = 40 # Number of second
CONNECTIONNAME = "0.8.35 2" # to see your connections, manually connect to router (telnet 192.168.1.1) and use "wan show" .You should choose you PPPoE connection Use a combination of VCC + Con Id. For example ""0.8.35 2""
tnt = telnetlib.Telnet(HOST)
tnt.read_until("Login: ")
tnt.write(LOGIN + "\n")
tnt.read_until("Password: ")
tnt.write(PASS + "\n")
tnt.read_until(PROMPT)
tnt.write("ppp config " + CONNECTIONNAME + " down\n")
time.sleep(3)
tnt.write("\n")
tnt.write("ppp config " + CONNECTIONNAME + " up\n")
time.sleep(WAITTIME)
tnt.read_until(PROMPT)
tnt.write("ppp config " + CONNECTIONNAME + " up\n")
tnt.write("\n")
tnt.read_until(PROMPT)
tnt.write("13\n") # press option to exit
tnt.close()
下面重启路由器:
#!/usr/bin/env python# -*- coding: utf-8 -*-
import sys
import telnetlib
import re
import time
import sys
import os
HOST = "192.168.2.1"
password = "password"
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
def drukuj(wiadomosc):
sys.stdout.write("\r")
sys.stdout.write(" ")
sys.stdout.flush()
sys.stdout.write("\r")
sys.stdout.write(wiadomosc)
sys.stdout.flush()
#sys.stdout.write("\r")
def status(komenda):
odb = ""
try:
tn = telnetlib.Telnet(host=HOST,timeout=23)
except IOError:
print "Nie nawiązano połączenia :(\n"
else:
tn.read_until("Password: ")
tn.write(password + "\n")
if komenda=="wan adsl reset\n":
tn.write(komenda)
tn.write("exit\n")
#print "Połączenie zostało z resetowane"
drukuj(bcolors.FAIL + "Połączenie zostało z resetowane" + bcolors.ENDC)
if komenda=="ip route status\n":
tn.write(komenda)
tn.write("exit\n")
odb = tn.read_all()
if komenda=="wan adsl status\n":
tn.write("wan adsl status\n")
tn.write("exit\n")
odb = tn.read_all()
if re.search('current modem status: down', odb):
#print "Status: down"
drukuj(bcolors.HEADER + "Status: down" + bcolors.ENDC)
if re.search('current modem status: wait for initialization', odb):
#print "Status: wait for initialization"
drukuj(bcolors.WARNING + "Status: wait for initialization" + bcolors.ENDC)
if re.search('current modem status: initializing', odb):
#print "Status: initializing"
drukuj(bcolors.WARNING + "Status: initializing" + bcolors.ENDC)
if re.search('current modem status: up', odb):
#print "Status: up"
drukuj(bcolors.OKBLUE + "Status: up" + bcolors.ENDC)
return odb
def clear():
if os.name == "posix":
# Unix/Linux/MacOS/BSD/etc
os.system('clear')
elif os.name in ("nt", "dos", "ce"):
# DOS/Windows
os.system('CLS')
if __name__=="__main__":
clear()
status("wan adsl reset\n")
time.sleep(5)
a = True
while a:
odb = status("ip route status\n")
if re.search('\d *poe0 *\d', odb):
#print "Modem działa"
drukuj(bcolors.OKGREEN + "-={{Modem działa}}=-\n" + bcolors.ENDC)
time.sleep(2)
#clear()
a = False
else:
time.sleep(3)
status("wan adsl status\n")
time.sleep(3)
下面通过telnet 来重启路由器
#!/usr/bin/env python# -*- coding: utf-8 -*-
import telnetlib
HOST = "192.168.1.1"
USER = "root"
PASS = "admin"
router = telnetlib.Telnet(HOST)
router.read_until(": ", 2) # wait for timeout or "BusyBox on localhost login: "
router.write(USER + "\n")
router.read_until(": ", 2) # wait for timeout or "Password: "
router.write(PASS + "\n")
router.read_until("# ", 2) # wait for timeout or "# "
router.write("reboot\n")
router.close()
print "Done!"
这是四段独立的代码!
简而言之,在python中,一个文件(以“.py”为后缀名的文件)就叫做一个模块,每一个模块在python里都被看作是一个独立的文件。模块可以被项目中的其他模块、一些脚本甚至是交互式的解析器所使用,它可以被其他程序引用,从而使用该模块里的函数等功能,使用Python中的标准库也是采用这种方法。