一些小功能,比较杂。
具体的功能在这里查看
1、本地环境的php运行结果获取。
2、快捷打开常用的文件,文件夹,url。(ctrl+shift+a)
3、常用的缩进转换。
下边是网络爬虫代码。
#py开启线程获取网络内容
class contrast_fileCommand(sublime_plugin.TextCommand):
def run(self,edit,toS=''):
if toS =='':return
shortDir='/lib/flyskyyun/bat_auto/php/'+toS+'/'
f_dir=common.sett.get('v_test_dir')+shortDir
pUrl=common.sett.get('v_test_url')
file_ar=os.listdir(f_dir)
self.runn=len(file_ar)
self.val=''
for f in file_ar:
url=pUrl+shortDir+f
thread = common.PrefixrApiCall(url,'html')
thread.start()
self.handle_threads(thread) def handle_threads(self, thread, i=0, dir=1 ):
before = i % 8
after = (7) - before
if not after: dir = -1
if not before: dir = 1
i += dir
if thread.is_alive():
self.view.set_status('prefixr', 'Prefixr [%s=%s]'% (' ' * before, ' ' * after))
sublime.set_timeout(lambda: self.handle_threads( thread, i, dir ), 100)
return
try: content=thread.result.decode('utf-8')
except :
try: content=thread.result.decode('gbk')
except :
print(thread.result)
content='编码错误'
try:
b_json=json.loads(content)
b_json=json.dumps(b_json,ensure_ascii=False,sort_keys=True, indent=2)
except :
b_json=content
self.runn-=1
self.val+=b_json if self.runn == 0:
self.view.erase_status('prefixr')
w = sublime.active_window()
r=w.create_output_panel("expec")
r.settings().set("word_wrap", True)
r.run_command('append', {'characters': self.val, 'force': True, 'scroll_to_end': True})
r.show(0)
w.run_command("show_panel", {"panel": "output.expec"}) # 获取网络内容
class PrefixrApiCall(threading.Thread):
def __init__(self,url,tr='json',typ=''):
self.typ = typ
self.url = url
self.tr = tr
self.result = ''
threading.Thread.__init__(self)
def run(self):
if self.typ == 'open_new':
print(1111)
import http.cookiejar
import urllib.parse
import urllib.error
tr=self.tr
cookie_filename = sett.get('v_plug_dir')+'/SubPhp/file/9.txt'
cookie = http.cookiejar.MozillaCookieJar(cookie_filename)
# FileCookieJar(filename):创建FileCookieJar实例,检索cookie信息并将信息存储到文件中,filename是文件名。
# MozillaCookieJar(filename):创建与Mozilla cookies.txt文件兼容的FileCookieJar实例。
# LWPCookieJar(filename):创建与libwww-perl Set-Cookie3文件兼容的FileCookieJar实例。
from http import cookies
cookie.load(cookie_filename, ignore_discard=True, ignore_expires=True)
openner=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
try:
headers={ 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE5.5; Windows NT)'}
text=open(sett.get('v_plug_dir')+'/SubPhp/file/0.txt').read()
text=reg_function(text,['ss','l','n'])
data=text.encode('utf-8')
# 转换编码
# self.url=urllib.parse.quote(self.url)
response=openner.open(self.url,data)
self.result=response.read()
# data=urllib.parse.urlencode(values).encode('utf-8')
# response=urllib.request.urlopen(urllib.request.Request(self.url, data, headers))
# self.result=response.read()
except urllib.error.HTTPError as e : # except urllib.error.HTTPError as e:
print(self.url)
print(self.result)
if tr=='json': self.result = '{"status": 0, "message":"网络获取失败"}'.encode('utf-8')
else: self.result = '页面错误'.encode('utf-8')
cookie.save(ignore_discard=True, ignore_expires=True) # 保存cookie到cookie.txt中
return