在Python中没有专用的SSH模块,这需要手动的安装模块才行。Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,需要安装模块的顺序是pycrypto-〉paramiko。
OpenSSH下载网址:http://sourceforge.net/projects/sshwindows/,下载安装即可。
Pycrypto模块下载地址:http://pypi.python.org/pypi/pycrypto/,下载安装时缺少vcvarsall.bat,提示需要VisualStudio,网上解决办法大部分是安装MingW32。
在网上找到已经编译好的Windows中使用的Pycrypto版本,下载网址为:
http://www.voidspace.org.uk/python/modules.shtml#pycrypto
下载Python版本和操作系统对应的版本,直接安装即可。
注:如果是Win32bit + Python 2.7,则下载pycrypto-2.6.win32-py2.7.exe。
使用SSH登陆到远程主机执行命令。
import paramiko def ssh_cmd(ip,port, cmd, user, passwd): result = "" try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip, port, user, passwd,timeout=3) stdin, stdout, stderr =ssh.exec_command(cmd) result = stdout.read() ssh.close() except: print("ssh_cmd err.") return result
http://stackoverflow.com/questions/1939107/python-libraries-for-ssh-handling
安装Scrapy:
pip install scrapy
报错:
only found improper OpenSSL directories:['E:\cygwin','E:\Program Files\Git']
答案:Apparently pyopenssl installation expects the binaries and libs to be laid out exactly as installed byOpenSSL windows binaries. Installing it from there (and not using cygwin's openssl for example), and adding the bin directory to the path solved this issue.(来自http://stackoverflow.com/questions/9151268/installing-scrapy-pyopenssl-in-windows-virtualenv)
看来要安装openssh,python中有专门的库pyopenssh
发现用上面的指令安装不是很好。
用下面的安装:
easy_install -U Scrapy (安装0.24版本easy_install -U "Scrapy= =0.24")
会自动下载pyopenssh和twisted库。Scrapy是基于 Twisted 实现的。
http://scrapy.org/
官网上的安装条件:
发现通过pip安装比较好, pip install 'Markdown<2.0' 指定版本= = ,>等。
Pre-requisites
The installation steps assume that you have the following things installed:
-
Python 2.6 or 2.7
-
OpenSSL. This comes preinstalled in all operating systems except Windows (see Platform specific installation notes)
-
pip or easy_install Python package managers
都安装成功后,运行:
scrapy startproject tutorial
报错:
File "E:ProgramFilespythonxypython27libsite-packageslxmlhtml\__init__.py", line 42, in <module>
from lxml import etree
ImportError: DLL load failed: 找不到指定的程序。
肯缺少lxml,去下载安装:https://pypi.python.org/pypi/lxml/3.2.1安装后这个错误没有了,又有新的错误:
File "E:ProgramFilespythonxypython27libsite-packages wistedwebhttp.py", line 76, in <module>
from urllib.parse import (
ImportError: No module named parse
我重新安装Scrapy:
可以看到scrapy依赖项:
Twisted
w3lib
lxml
pyOpenssl
zope.interface
没办法,卸载Scrapy:
pip uninstall SCrapy
然后单独下载安装:
twisted http://twistedmatrix.com/trac/ 下载 python2.7的版本
w3lib https://github.com/scrapy/w3lib
lxml:
pyOpenssl:https://pypi.python.org/pypi/pyOpenSSL
zopen.interface
参考:http://www.tuicool.com/articles/NzAF7n
经过以上重装后还是错误:
from urllib.parse import (
ImportError: No module named parse
不知道怎么办了?stackoverflow上也有一个人跟我有同样的错误:
http://stackoverflow.com/questions/15011205/cant-install-pip-due-to-importerror-no-module-named-parse
在windows下安装scrapy的方法详解
twiseted 教程http://blog.sina.com.cn/s/blog_704b6af70100py9n.html
安装Scrapy 步骤:http://www.crifan.com/install_scrapy/
http://www.cnblogs.com/CLTANG/archive/2011/07/05/2098531.html
找不到Python.h文件,ubutnu要安装:
apt-get install python-dev
ubuntion通过easy_install -U scrapy报错。
无法打开包括文件:“openssl/aes.h”: No such file or directory。
解决方法:
sudo apt-get install libssl0.9.8
sudo apt-get install libssl-dev
sudo apt-get install libssh-dev
至此,再次运行sudo easy_install pyopenssl 成功安装pyopenssl。(ubuntu python版本:Python 2.7.6).
看了easy_install也不能完全处理依赖关系啊。
参考:http://yueyizx.blog.51cto.com/4006053/1430518
http://www.cnblogs.com/ffan/p/3870324.html