Python:Network(example coding)+ FTP
本文会涉及到两个服务器:
1. FTP server:vsftpd。
192.168.232.142
2. Python Client。
192.168.232.145
用极简的方式搭建一个初步的FTP服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
[root@cobbler-server ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cobblerserver-lv_root 50G 17G 30G 37% / tmpfs 931M 72K 931M 1% /dev/shm /dev/sda1 477M 34M 419M 8% /boot /dev/mapper/vg_cobblerserver-lv_home 5.4G 12M 5.1G 1% /home [root@cobbler-server ~]# [root@cobbler-server ~]# mount /dev/sr0 /iso mount: block device /dev/sr0 is write-protected, mounting read-only [root@cobbler-server ~]# [root@cobbler-server ~]# yum repolist Loaded plugins: product-id, refresh-packagekit, security, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. HighAvailability | 4.1 kB 00:00 ... HighAvailability/primary_db | 44 kB 00:00 ... ISO | 4.1 kB 00:00 ... ISO/primary_db | 3.1 MB 00:00 ... LoadBalancer | 4.1 kB 00:00 ... LoadBalancer/primary_db | 7.4 kB 00:00 ... ResilientStorage | 4.1 kB 00:00 ... ResilientStorage/primary_db | 50 kB 00:00 ... ScalableFileSystem | 4.1 kB 00:00 ... ScalableFileSystem/primary_db | 7.0 kB 00:00 ... Server | 4.1 kB 00:00 ... Server/primary_db | 3.1 MB 00:00 ... repo id repo name status HighAvailability HighAvailability 55 ISO ISO 3,785 LoadBalancer LoadBalancer 4 ResilientStorage ResilientStorage 62 ScalableFileSystem ScalableFileSystem 7 Server Server 3,785 epel Extra Packages for Enterprise Linux 6 - x86_64 12,151 repolist: 19,849 [root@cobbler-server ~]# [root@cobbler-server ~]# yum list | grep --color vsftp vsftpd.x86_64 2.2.2-12.el6_5.1 ISO [root@cobbler-server ~]# [root@cobbler-server ~]# yum install vsftpd Loaded plugins: product-id, refresh-packagekit, security, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package vsftpd.x86_64 0:2.2.2-12.el6_5.1 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================ Package Arch Version Repository Size ================================================================================================================================================ Installing: vsftpd x86_64 2.2.2-12.el6_5.1 ISO 151 k Transaction Summary ================================================================================================================================================ Install 1 Package(s) Total download size: 151 k Installed size: 331 k Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : vsftpd-2.2.2-12.el6_5.1.x86_64 1/1 HighAvailability/productid | 1.6 kB 00:00 ... ISO/productid | 1.6 kB 00:00 ... LoadBalancer/productid | 1.6 kB 00:00 ... ResilientStorage/productid | 1.6 kB 00:00 ... ScalableFileSystem/productid | 1.6 kB 00:00 ... Server/productid | 1.6 kB 00:00 ... Verifying : vsftpd-2.2.2-12.el6_5.1.x86_64 1/1 Installed: vsftpd.x86_64 0:2.2.2-12.el6_5.1 Complete! [root@cobbler-server ~]# [root@cobbler-server ~]# rpm -qa | grep ftp tftp-server-0.49-7.el6.x86_64 gvfs-obexftp-1.4.3-18.el6.x86_64 vsftpd-2.2.2-12.el6_5.1.x86_64 [root@cobbler-server ~]# [root@cobbler-server ~]# rpm -ql vsftpd /etc/logrotate.d/vsftpd /etc/pam.d/vsftpd /etc/rc.d/init.d/vsftpd /etc/vsftpd /etc/vsftpd/ftpusers /etc/vsftpd/user_list /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd_conf_migrate.sh /usr/sbin/vsftpd /usr/share/doc/vsftpd-2.2.2 /usr/share/doc/vsftpd-2.2.2/AUDIT /usr/share/doc/vsftpd-2.2.2/BENCHMARKS /usr/share/doc/vsftpd-2.2.2/BUGS /usr/share/doc/vsftpd-2.2.2/COPYING /usr/share/doc/vsftpd-2.2.2/Changelog /usr/share/doc/vsftpd-2.2.2/EXAMPLE /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README.configuration /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.conf /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.xinetd /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/README /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf /usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG /usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/README /usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/README.configuration /usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/hosts.allow /usr/share/doc/vsftpd-2.2.2/EXAMPLE/README /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_HOSTS /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_HOSTS/README /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/README /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/README.configuration /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/logins.txt /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS_2 /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS_2/README /usr/share/doc/vsftpd-2.2.2/FAQ /usr/share/doc/vsftpd-2.2.2/INSTALL /usr/share/doc/vsftpd-2.2.2/LICENSE /usr/share/doc/vsftpd-2.2.2/README /usr/share/doc/vsftpd-2.2.2/README.security /usr/share/doc/vsftpd-2.2.2/REWARD /usr/share/doc/vsftpd-2.2.2/SECURITY /usr/share/doc/vsftpd-2.2.2/SECURITY/DESIGN /usr/share/doc/vsftpd-2.2.2/SECURITY/IMPLEMENTATION /usr/share/doc/vsftpd-2.2.2/SECURITY/OVERVIEW /usr/share/doc/vsftpd-2.2.2/SECURITY/TRUST /usr/share/doc/vsftpd-2.2.2/SIZE /usr/share/doc/vsftpd-2.2.2/SPEED /usr/share/doc/vsftpd-2.2.2/TODO /usr/share/doc/vsftpd-2.2.2/TUNING /usr/share/doc/vsftpd-2.2.2/vsftpd.xinetd /usr/share/man/man5/vsftpd.conf.5.gz /usr/share/man/man8/vsftpd.8.gz /var/ftp /var/ftp/pub [root@cobbler-server ~]# [root@cobbler-server vsftpd]# pwd /etc/vsftpd [root@cobbler-server vsftpd]# [root@cobbler-server vsftpd]# ls -ltr total 20 -rwxr--r-- 1 root root 338 Jul 29 2014 vsftpd_conf_migrate.sh -rw------- 1 root root 4599 Jul 29 2014 vsftpd.conf -rw------- 1 root root 126 Jul 18 20:51 ftpusers -rw------- 1 root root 362 Jul 18 20:53 user_list [root@cobbler-server vsftpd]# [root@cobbler-server vsftpd]# grep -C 2 "root" ftpusers # Users that are not allowed to login via ftp #root bin daemon [root@cobbler-server vsftpd]# [root@cobbler-server vsftpd]# grep -C 2 "root" user_list # Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers # for users that are denied. #root bin daemon [root@cobbler-server vsftpd]# [root@cobbler-server vsftpd]# service vsftpd status vsftpd is stopped [root@cobbler-server vsftpd]# service vsftpd start Starting vsftpd for vsftpd: [ OK ] [root@cobbler-server vsftpd]# [root@cobbler-server vsftpd]# service vsftpd status vsftpd (pid 3739) is running... [root@cobbler-server vsftpd]# [root@cobbler-server vsftpd]# netstat -tupln | grep --color ftp tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 3739/vsftpd [root@cobbler-server vsftpd]# |
这样,最基本的FTP服务就配置完了。
Python Client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
root@philosopher:~# pwd /root root@philosopher:~# ls Desktop Documents Downloads Music Pictures Public Templates Videos root@philosopher:~# root@philosopher:~# ftp 192.168.232.142 Connected to 192.168.232.142. 220 (vsFTPd 2.2.2) Name (192.168.232.142:root): root 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> pwd 257 "/root" ftp> ls 200 PORT command successful. Consider using PASV. 150 Here comes the directory listing. drwxr-xr-x 2 0 0 4096 Mar 08 10:32 Desktop drwxrwxr-x 9 1000 1000 4096 Mar 05 14:24 Django-1.9.4 -rw-r--r-- 1 0 0 7426995 Mar 05 14:26 Django-1.9.4.tar.gz drwxr-xr-x 2 0 0 4096 Mar 08 10:32 Documents drwxr-xr-x 2 0 0 4096 Mar 08 10:32 Downloads drwxr-xr-x 2 0 0 4096 Mar 08 10:32 Music drwxr-xr-x 2 0 0 4096 Mar 08 10:32 Pictures drwxr-xr-x 2 0 0 4096 Mar 08 10:32 Public -rw-r--r-- 1 0 0 3440640 Mar 08 11:09 Python-3.5.1.tgz drwxr-xr-x 2 0 0 4096 Mar 08 10:32 Templates drwxr-xr-x 2 0 0 4096 Mar 08 10:32 Videos -rw------- 1 0 0 2942 Mar 08 10:26 anaconda-ks.cfg -rw-r--r-- 1 0 0 15710 Mar 08 10:34 cobbler.sh drwxr-xr-x 3 0 0 4096 Mar 08 10:52 git_data -rw-r--r-- 1 0 0 52869 Mar 08 10:26 install.log -rw-r--r-- 1 0 0 9963 Mar 08 10:24 install.log.syslog -rw-r--r-- 1 0 0 3255 Mar 27 14:16 ntp.sh 226 Directory send OK. ftp> ftp> get Django-1.9.4.tar.gz local: Django-1.9.4.tar.gz remote: Django-1.9.4.tar.gz 200 PORT command successful. Consider using PASV. 150 Opening BINARY mode data connection for Django-1.9.4.tar.gz (7426995 bytes). 226 Transfer complete. 7426995 bytes received in 0.15 secs (46.2961 MB/s) ftp> ftp> bye 221 Goodbye. root@philosopher:~# root@philosopher:~# ls -ltr total 7288 drwxr-xr-x 2 root root 4096 Jul 11 00:27 Desktop drwxr-xr-x 2 root root 4096 Jul 11 00:27 Videos drwxr-xr-x 2 root root 4096 Jul 11 00:27 Templates drwxr-xr-x 2 root root 4096 Jul 11 00:27 Public drwxr-xr-x 2 root root 4096 Jul 11 00:27 Pictures drwxr-xr-x 2 root root 4096 Jul 11 00:27 Music drwxr-xr-x 2 root root 4096 Jul 11 00:27 Downloads drwxr-xr-x 2 root root 4096 Jul 11 00:53 Documents -rw-r--r-- 1 root root 7426995 Jul 18 21:06 Django-1.9.4.tar.gz root@philosopher:~# |
可以看到:FTP服务工作正常,客户端可以从服务端获取文件。
下面,编写Python代码,完成类似的事情。
先查看Python的版本:
1 2 3 4 5 6 |
root@philosopher:~# whereis python python: /usr/bin/python2.7-config /usr/bin/python /usr/bin/python3.4m /usr/bin/python2.7 /usr/bin/python3.4 /usr/lib/python3.5 /usr/lib/python2.7 /usr/lib/python2.6 /usr/lib/python3.4 /etc/python /etc/python3.5 /etc/python2.7 /etc/python3.4 /usr/local/lib/python2.7 /usr/local/lib/python3.4 /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz root@philosopher:~# root@philosopher:~# python -V Python 2.7.11 root@philosopher:~# |
Python Network Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
root@philosopher:~# python Python 2.7.11 (default, Dec 9 2015, 00:29:25) [GCC 5.3.1 20151205] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import socket >>> socket.setdefaulttimeout(2) >>> s = socket.socket() >>> s.connect(("192.168.232.142",21)) >>> ans = s.recv(1024) >>> print ans 220 (vsFTPd 2.2.2) >>> |
这段代码如果加入异常处理,则是下面的形态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
root@philosopher:/script# pwd /script root@philosopher:/script# ls -ltr * -rw-r--r-- 1 root root 155 Jul 18 21:42 ftp_except.py root@philosopher:/script# root@philosopher:/script# cat ftp_except.py import socket socket.setdefaulttimeout(2) s=socket.socket() try: s.connect(("192.168.232.141",21)) except Exception, e: print "Except is:: "+str(e) root@philosopher:/script# root@philosopher:/script# python ftp_except.py Except is:: timed out root@philosopher:/script# |
添加函数后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
root@philosopher:/script# cat ftp_function.py import socket def retBanner(ip,port): try: socket.setdefaulttimeout(2) s = socket.socket() s.connect((ip,port)) banner = s.recv(1024) return banner except Exception, e: print "Error:: IP:: " + ip + " on Port:: " + str(port) + " Except:: " + str(e) return def main(): ip1 = "192.168.232.141" ip2 = "192.168.232.142" port = 21 banner1 = retBanner(ip1,port) if banner1: print "A:: " + ip1 + " Banner:: " + banner1 banner2 = retBanner(ip2,port) if banner2: print "B:: " + ip2 + " Banner:: " + banner2 if __name__ == '__main__': main() root@philosopher:/script# root@philosopher:/script# python ftp_function.py Error:: IP:: 192.168.232.141 on Port:: 21 Except:: timed out B:: 192.168.232.142 Banner:: 220 (vsFTPd 2.2.2) root@philosopher:/script# |
——————————————
Done。