如何制作 python FTP 服务器 PUBLIC?
How do I make a python FTP server PUBLIC?
我已经用 Pyftpdlib 创建了一个 FTP 服务器,我希望能够从另一个 country/IP 连接到服务器,我已经尝试上传我的脚本(以 exe 形式)发送给该国另一地区的朋友,我无法连接(简单 timeout/invalid 错误)。如何连接到不在我附近的 ftp 服务器?
服务器
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import requests
import mysql.connector
sqlpass = "THIS ISNT THE PASSWORD"
def ftp_main():
mydb = mysql.connector.connect(
host="remotemysql.com",
port="3306",
user="NOT TELLING",
passwd=sqlpass,
database="NO THANKS"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT Username, Password FROM NO-THANKS")
myresult = mycursor.fetchall()
Username, Password = myresult[0]
print(Username + " " + Password)
authorizer = DummyAuthorizer()
authorizer.add_user(Username, Password, 'C:/', perm='elradfmwMT')
authorizer.add_anonymous('C:/')
handler = FTPHandler
handler.authorizer = authorizer
handler.banner = "Test Banner"
# Instantiate FTP server class and listen on 0.0.0.0:2121
address = ('0.0.0.0', 21)
server = FTPServer(address, handler)
# start ftp server
server.serve_forever()
ftp_main()
客户端
from ftplib import FTP
ftp = FTP('104.193.20.211')
ftp.login("user", "pass")
ftp.retrlines('LIST')
ftp.cwd("Users/TEST/Desktop")
ftp.retrlines('LIST')
getfile = 'test1.txt'
command = 'RETR'
ftp.retrbinary(command + " " + getfile, open('test1.txt', 'wb').write)
我希望连接并能够列出文件,但我就是连接不上。
(根据你所说的,我认为只有当你连接到远程网络时它才有效。)
因为您的连接有问题,看来是网络问题。从编程的角度来看,您是否连接到本地网络上的 IP 应该无关紧要。
检查这些可能的问题:
- 服务器 NAT: Typical consumer router performs network address translation between public and private addresses. It's useful when your ISP provides you with single IP, but you want to use multiple devices. When typical home router sees packet from local computer to somewhere in internet, it changes it's source IP to IP provided by your ISP, changes source port if necessary, and remembers this connection. Router can later use this remembered information to translate everything back when reply arrives. In this case, you can create connections only from inside of local network. If you want to receive connection from internet, you'll have to explicitly set which computer on local network should receive this connection. Search for "port forwarding" or "virtual servers" or something similar in routers settings. Some protocols (like FTP) have problems 支持 NAT,您的路由器需要支持这些应用程序协议。
- 网络某处有防火墙,或服务器端OS不允许使用低端口号(这种可能性较小。您可能会在服务器端遇到一些错误)。
我已经用 Pyftpdlib 创建了一个 FTP 服务器,我希望能够从另一个 country/IP 连接到服务器,我已经尝试上传我的脚本(以 exe 形式)发送给该国另一地区的朋友,我无法连接(简单 timeout/invalid 错误)。如何连接到不在我附近的 ftp 服务器?
服务器
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import requests
import mysql.connector
sqlpass = "THIS ISNT THE PASSWORD"
def ftp_main():
mydb = mysql.connector.connect(
host="remotemysql.com",
port="3306",
user="NOT TELLING",
passwd=sqlpass,
database="NO THANKS"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT Username, Password FROM NO-THANKS")
myresult = mycursor.fetchall()
Username, Password = myresult[0]
print(Username + " " + Password)
authorizer = DummyAuthorizer()
authorizer.add_user(Username, Password, 'C:/', perm='elradfmwMT')
authorizer.add_anonymous('C:/')
handler = FTPHandler
handler.authorizer = authorizer
handler.banner = "Test Banner"
# Instantiate FTP server class and listen on 0.0.0.0:2121
address = ('0.0.0.0', 21)
server = FTPServer(address, handler)
# start ftp server
server.serve_forever()
ftp_main()
客户端
from ftplib import FTP
ftp = FTP('104.193.20.211')
ftp.login("user", "pass")
ftp.retrlines('LIST')
ftp.cwd("Users/TEST/Desktop")
ftp.retrlines('LIST')
getfile = 'test1.txt'
command = 'RETR'
ftp.retrbinary(command + " " + getfile, open('test1.txt', 'wb').write)
我希望连接并能够列出文件,但我就是连接不上。
(根据你所说的,我认为只有当你连接到远程网络时它才有效。)
因为您的连接有问题,看来是网络问题。从编程的角度来看,您是否连接到本地网络上的 IP 应该无关紧要。
检查这些可能的问题:
- 服务器 NAT: Typical consumer router performs network address translation between public and private addresses. It's useful when your ISP provides you with single IP, but you want to use multiple devices. When typical home router sees packet from local computer to somewhere in internet, it changes it's source IP to IP provided by your ISP, changes source port if necessary, and remembers this connection. Router can later use this remembered information to translate everything back when reply arrives. In this case, you can create connections only from inside of local network. If you want to receive connection from internet, you'll have to explicitly set which computer on local network should receive this connection. Search for "port forwarding" or "virtual servers" or something similar in routers settings. Some protocols (like FTP) have problems 支持 NAT,您的路由器需要支持这些应用程序协议。
- 网络某处有防火墙,或服务器端OS不允许使用低端口号(这种可能性较小。您可能会在服务器端遇到一些错误)。