ubuntu 配置ftp 服务器 并访问

ubuntu 配置 ftp 服务器

  • (1)在 Ubuntu 中安装 VSFTPD 服务器
sudo apt-get update
sudo apt-get install vsftpd

一旦安装完成,初始情况下服务被禁用。因此,我们需要手动开启服务,同时,启动它使得在下次开机时能够自动开启服务:

systemctl start vsftpd
systemctl enable vsftpd

service vsftpd start
chkconfig --level 35 vsftpd on
  • (2)在 Ubuntu 中配置并保护 VSFTPD 服务器
(a) 修改配置文件 vsftpd.conf
sudo vim /etc/vsftpd.conf

修改内容如下:
listen=YES
#listen_ipv6=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=077
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
#chown_uploads=YES
#chown_username=whoever
xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES
#idle_session_timeout=600
#data_connection_timeout=120
#nopriv_user=ftpsecure
#async_abor_enable=YES
ascii_upload_enable=YES
ascii_download_enable=YES
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd.banned_emails
chroot_local_user=YES
chroot_local_user=NO
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
#ls_recurse_enable=YES
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
utf8_filesystem=YES
local_root=/home/rpr/ftp_files #!!!!!change
allow_writeable_chroot=YES
userlist_enable=YES                   # vsftpd 将会从所给的用户列表文件中加载用户名字列表
userlist_file=/etc/vsftpd.userlist    # 存储用户名字的列表
userlist_deny=NO
(b)添加账户
sudo useradd -m -c "rpr" -s /bin/bash rprobot
sudo passwd rprobot

echo "rprobot" | sudo tee -a /etc/vsftpd.userlist
cat /etc/vsftpd.userlist
  • (3)连接ftp服务器
ubuntu: ftp 192.168.1.136
windows: ftp://rprobot:123@192.168.1.136:21

使用代码访问 ftp 服务器

  • python 【ftplib】
import cv2
import os
from ftplib import FTP

def ftp_conenct(host, username, password):
    # ftp 登录
    ftp = FTP()
    ftp.connect(host, 21)
    ftp.login(username, password)
    print(ftp.getwelcome())
    return ftp

def upload_file(ftp, remote_path, local_path):
    bufsize = 1024
    fp = open(local_path, 'rb')
    ftp.storbinary('STOR' + remote_path, fp, bufsize)
    ftp.set_debuglevel(0)
    fp.close()

if __name__ == "__main__":

    os.remove('./img.jpg')

    #登录ftp服务器
    ftp = ftp_conenct("192.168.1.136", "rprobot", "123")
    #ftp = ftp_conenct("192.168.1.104", "rpr", "f")

    #截取图片并上传
    cap = cv2.VideoCapture(0)
    img = cap.read()[1]
    cv2.imwrite('./img.jpg', img)
    fp = open('./img.jpg', 'rb')
    ftp.storbinary('STOR InfareCamera/img.jpg', fp)
    ftp.close()
    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值