惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
Schneier on Security
Schneier on Security
SecWiki News
SecWiki News
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
O
OpenAI News
W
WeLiveSecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Hacker News: Front Page
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
P
Privacy & Cybersecurity Law Blog
K
Kaspersky official blog
S
Security @ Cisco Blogs
Latest news
Latest news
AWS News Blog
AWS News Blog
U
Unit 42
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Know Your Adversary
Know Your Adversary
Scott Helme
Scott Helme
博客园 - 司徒正美
B
Blog RSS Feed
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker
Google Online Security Blog
Google Online Security Blog
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Last Week in AI
Last Week in AI
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
SegmentFault 最新的问题
NISL@THU
NISL@THU
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
小众软件
小众软件

博客园 - snryang

网站发布小工具,--让发布变简单一点 - snryang - 博客园 关于跑步耳机的一些思考 对棋牌游戏平台的一些思考 复杂业务简单化的一个小技巧 老婆和老媽同時掉在了水裡终于有答案了 重发我的 HTML单据输入控件 js对象序列化为json字符串 CSS的一个小技巧 - snryang - 博客园 基于jQuery的单据输入 基于jQuery的表单验证 - snryang - 博客园 基于JQuery的拖拉效果, cms系统也不复杂 IList对象排序方法 基于jqury的自动完成 让你的博客园变灰 弹出遮罩层示例. Jquery学习 用反射来解决字段多带来的烦恼 数据库结构中的"树"
鼠标右键发布文件到远程服务器
snryang · 2016-11-23 · via 博客园 - snryang

鼠标右键发布文件到远程服务器

2016-11-23 18:03  snryang  阅读(536)  评论()    收藏  举报

经常需要单个更新文件到Web服务器,总是需要远程链接,复制,粘贴,还要核对目录层次,太繁琐。
所以我想,当我修改某个文件后,点击右键,选择postFile即可将选中的文件上传到服务器。

需要服务器端支持,我的是.net项目,所以直接在项目中增加了以下代码

    [HttpPost]
    [ValidateInput(false)]
    public ContentResult PostFile(string file, string content, string sign) {
        if (sign != "123") return Content("error");
        var filePath = Server.MapPath(file);
        System.IO.File.WriteAllText(filePath, content);
        return Content("ok");            
    }

file:上传文件的相对路径 content:文件内容 sign:可有可无,签名验证,根据团队情况处理。
注意:需要为服务器的IIS_user用户配置写文件的权限。

服务器端所做的工作:接收路径,文件内容,然后必定文件。路径需要根据项目实际情况做约定。(用Node.js来做服务器端会更好)

客户端工作:
1.修改注册表增加一个鼠标右键菜单,右键菜单执行一个批处理文件,并传入选择的文件路径

Windows Registry Editor Version 5.00  
[HKEY_CLASSES_ROOT\*\shell\postFile\command]  
@="postFile.bat \"%1\""  

‘postFile’为菜单名称 poftFile.bat为批处理文件名称,需要放到C:\windows目录下

2.批处理文件中执行Python脚本,并传入文件路径参数

c:\Python27\python.exe d:\postFile.py %*

3.在Python脚本中,调用Http请求上传文件,相关路径需要根据项目约定做处理。

#! usr/bin/python
#coding=utf-8

import sys  
import requests
if __name__ == '__main__':  
    if len(sys.argv)!= 2:  
        sys.exit('argv error!')
#filepath = sys.argv[1]
#posturl='http://xxxx/Home/PostFile'
posturl='http://xxx/Home/PostFile' #测试环境
#posturl='http://xxx/PostFile' #预发布

filepath = sys.argv[1]
webOrAdmin = ''
relativePath = ''
ext = ''
print filepath
if "\\Jwell.Web\\" in filepath:
    print 'web'
    webOrAdmin = 'web'
if "\\Jwell.Admin\\" in filepath:
    print 'admin'
    webOrAdmin = 'admin'
if webOrAdmin == '':
    sys.exit('webOrAdmin error!')
if webOrAdmin == 'web':
    relativePath = filepath[filepath.index("\\Jwell.Web\\")+10:]
    print relativePath
if webOrAdmin == 'admin':
    relativePath = filepath[filepath.index("\\Jwell.Admin\\")+12:]
    print relativePath
    sys.exit('admin not support!')
ext = relativePath[relativePath.rindex('.')+1:]
print 'ext=' + ext
if ext in ['css','js','cshtml']:
    content = open(filepath, "r").read()
    print content
    postdata = {'file': relativePath, 'content': content,'sign':'123'}
    r = requests.post(posturl, data=postdata)
    print r.text
str = raw_input("任意键退出: ")

python脚本中根据项目结构,获取文件相对于某个目录的‘相对路径’,然后将相对路径,文件内容发送到发送指定的URL,只允许上传css js cshtml文件。需要根据您的实际项目情况做相应的处理。

最后整理成一个批处理文件,团队中需要安装Python,运行一次批处理文件即可。

    copy d:\postFile\postFile.py d:\postFile.py
    copy d:\postFile\postFile.bat c:\Windows\postFile.bat
    xcopy d:\postFile\requests\*.* c:\Python27\Lib\requests /s /i
    d:\postFile\postFile.Reg
    pause

python下载地址:https://www.python.org/downloads/release/python-2712/
源代码:https://git.oschina.net/nxwsyang/postfile.git