python—os.system()与subprocess.call()使用
参考:https://www.cnblogs.com/songwenlong/p/5940155.html
实例演示:
打开记事本:
1 import os
2 os.system('notepad')
或
1 import subprocess
2 subprocess.call('notepad')
我们看以下代码:
1 import os,re
2 os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe"')
这段代码会启动网易云音乐,效果和我们在cmd窗口中输入 "D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe" 效果一样。注意字符串中含有空格,所以有 r''。
而以下代码也可以实现同样的功能:
1 import subprocess
2 subprocess.call("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe")
同上面那段代码的区别只是括号中的 r''。
到目前为止一切正常,我们再看下面的代码,尝试着同时打开两个程序:
1 import os,re
2 os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"')
3 或
4 os.system("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad")
5 或
6 os.system(""D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"")
以上尝试都不会成功。
换做subprocess.call()函数也不能实现。
这个问题早在07年就有人提交过,请参考http://bugs.python.org/issue1524
root@kali:~/python# cat login2.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import string
import urllib
import urllib2
import requests
url = 'http://192.168.40.239/wordpress3.2/wp-login.php'#请求的登陆的url地址
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'#浏览器模式
values = {'log' : 'xwbk12',
'pwd' : '173605852',
'wp-submit' : "登录",
'redirect_to' : 'http://192.168.40.239/wordpress3.2/wp-admin/',
'testcookie' : '1'
}#请求的URL地址,post表单数据信息
headers = { 'Host' : '192.168.40.239',
'User-Agent' : user_agent,
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' : 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding' : 'gzip, deflate',
'Referer' : 'http://192.168.40.239/wordpress3.2/wp-login.php',
'Cookie' : 'wp-settings-time-1=1512642868; wordpress_test_cookie=WP+Cookie+check',
'Connection' : 'keep-alive',
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : '135'}#请求的头部信息
data = urllib.urlencode(values)#请求post表单数据
req = urllib2.Request(url,data)#请求数据)
response = urllib2.urlopen(req)#打开请求的数据
the_page = response.read()#读取并缓存请求到的数据
print the_page#打印请求到的页面
print "你请求到页面数据包为%d字节" %len(the_page)#计算请求到的页面数据大小
#参考https://www.cnblogs.com/jackyspy/p/6027385.html
proxy_handler = urllib2.ProxyHandler({'http': '192.168.40.36:4455'})
opener = urllib2.build_opener(proxy_handler)
r = opener.open(url)
print(r.read())
'''
if len(the_page) == 1048:#数据包大小等于1048就是成功
print "loing success!!!"
else:
print "login fail!!!!"
'''
root@kali:~/python#
root@kali:~/python# cat login2.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import string
import urllib
import urllib2
import requests
url = 'http://192.168.40.239/wordpress3.2/wp-login.php'#请求的登陆的url地址
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'#浏览器模式
values = {'log' : 'xwbk12',
'pwd' : '173605852',
'wp-submit' : "登录",
'redirect_to' : 'http://192.168.40.239/wordpress3.2/wp-admin/',
'testcookie' : '1'
}#请求的URL地址,post表单数据信息
headers = { 'Host' : '192.168.40.239',
'User-Agent' : user_agent,
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' : 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding' : 'gzip, deflate',
'Referer' : 'http://192.168.40.239/wordpress3.2/wp-login.php',
'Cookie' : 'wp-settings-time-1=1512642868; wordpress_test_cookie=WP+Cookie+check',
'Connection' : 'keep-alive',
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : '135'}#请求的头部信息
data = urllib.urlencode(values)#请求post表单数据
req = urllib2.Request(url,data)#请求数据)
proxy_handler = urllib2.ProxyHandler({'http': '192.168.40.36:4455'})#代理抓包
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)#使用post请求方法
response = urllib2.urlopen(req)#打开请求的数据
the_page = response.read()#读取并缓存请求到的数据
print the_page#打印请求到的页面
print "你请求到页面数据包为%d字节" %len(the_page)#计算请求到的页面数据大小
'''
if len(the_page) == 1048:#数据包大小等于1048就是成功
print "loing success!!!"
else:
print "login fail!!!!"
'''
root@kali:~/python#
root@kali:~/python/laowangpy/function# cat postloginxsser_bk.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import string
import urllib
import urllib2
url = 'http://192.168.40.239/xsser/index.php?do=login&act=submit'#请求的登陆的url地址
host = "192.168.40.239"
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
accept_language = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"
accept_encoding = "gzip, deflate"
referer = "http://192.168.40.239/xsser/index.php?do=login"
cookie = "PHPSESSID=oe42uceauanna7vqi4bpoodr63; security=impossible"
connection = "keep-alive"
content_type = "application/x-www-form-urlencoded"
content_length = "135"
values = {'user' : 'xwb',
'pwd' : 'qwe123456'
}#请求的URL地址,post表单数据信息
headers = {"Host" : host,"User-Agent" : user_agent,"Accept" : accept,"Accept-Language" : accept_language,"Accept-Encoding" : accept_encoding,"Referer" : referer,"Cookie" : cookie,"Connection" : connection,"Content-Type" : content_type,"Content-Length" : content_length}
data = urllib.urlencode(values)#请求post表单数据
req = urllib2.Request(url,data,headers)#请求数据)
'''
req.add_header('Host','192.168.40.239')
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0')
req.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
req.add_header('Accept-Language','zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3')
req.add_header('Accept-Encoding','gzip, deflate')
req.add_header('Referer','http://192.168.40.239/xsser/index.php?do=login')
req.add_header('Cookie','PHPSESSID=oe42uceauanna7vqi4bpoodr63; security=impossible')
req.add_header('Connection','keep-alive')
req.add_header('Content-Type','application/x-www-form-urlencoded')
req.add_header('Content-Length','135')#请求的头部信息
'''
proxy_handler = urllib2.ProxyHandler({'http': '192.168.40.1:4455'})#抓包
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)#启用post请求
response = urllib2.urlopen(req)#打开请求的数据
the_page = response.read()#读取并缓存请求到的数据
print the_page#打印请求到的页面
print "你请求到页面数据包为%d字节" %len(the_page)#计算请求到的页面数据大小
root@kali:~/python/laowangpy/function#
POST的请求参考:
https://www.cnblogs.com/lps365/p/3317995.html
https://www.cnblogs.com/testcoffee/p/6295911.html
http://blog.csdn.net/caoshuming_500/article/details/51500794
http://blog.csdn.net/wangkjs/article/details/47444599