STOR命令的正确用户 [英] proper user of STOR command

查看:123
本文介绍了STOR命令的正确用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从ftp服务器获取响应消息我正在解决一个连接问题,因此我使用的是PHP的 ftp_raw 函数,它允许我发送原始的ftp命令到远程服务器,并返回响应字符串。 (内置的PHP ftp命令不会返回响应:(b / b)

关注这个接受的答案,我发送的命令是

  PASV 
STOR /local/path/to/file.txt

而服务器响应是:

  500 /local/path/to/file.txt:系统找不到指定的路径

我自己在想:当然,远程主机不知道我的本地文件系统。My hunch是我打开一个套接字,指定一个远程文件名,并且我仍然需要传输数据,但是在我的搜索文档中,我没有发现任何确凿的结果。



什么是上传文件的完整的原始ftp命令集?在什么时候,以及如何实际开始向远程服务器发送数据?我可以使用从 FTP_CONNECT()作为套接字?

在FTP开始流行之后,写了一段时间,并且还有一些基于缺乏规范的破解软件(以及一些之后)RFC959发布。许多较老的(并且更稳定的)FTP库对于某些服务器有一些特殊的处理,以确保它以99.9%的时间工作。这对于处理FTP协议的扩展特别常见。



我的答案的其余部分假设服务器符合RFC959。



另外请记住,绕过FTP客户端库的更高级别的请求/响应管理意味着您需要自己重新实现这个库的一部分。这意味着你应该对规范感到满意,因为你需要参考它。在可能的情况下,我会参考适当的部分,以便您可以避开。



在某些情况下,我强烈建议您通过步进到PHP的FTP客户端库中,而不是自己实现所有这些。这是可能的,你应该真的要求库输出它使用的所有命令。所有这些,我仍然会引导你完成帮助诊断你的问题的程序。

FTP数据连接有点痛苦。如果您想支持规范的所有可选部分,它并不像乍看起来那么容易。具体如何传输文件主要取决于以下选项的当前状态:


  1. 数据类型 3.1.1):使用图像/二进制数据类型传输文件通常是最安全和最有效的。这不是默认设置,某些FTP命令(如目录列表)要求将其设置为ASCII,因此请确保您始终在传输前将其设置为

  2. 数据结构(第3.1.2节):文件结构通常是您想要的,但一些较旧的计算机和大型机可能必须转换为此模式并从此模式转换。

  3. 传输模式(第3.4节):流模式是最常用的模式,但块模式支持恢复中断传输,压缩模式没有多大意义。

  4. 连接模式(3.2节和3.3节):客户端或服务器可以通过连接到对等端来建立数据连接。这必须使用以下任一方式进行协商:

    • default :客户端在端口20上侦听;或

    • 自定义端口:客户端通知服务器其在另一个端口上列出;或

    • 被动模式:客户端询问服务器将在哪个端口上进行侦听。
    • b $ b

请注意规格,因为某些配置允许您保持数据连接打开,而其他配置可能要求您关闭它(例如流模式)。如果数据连接已经打开,那么您不需要在每次传输时重新连接到服务器。






全部这似乎很复杂,但它只是提供信息。它在调试时可能会派上用场。实际上,只有两种使用FTP传输文件的流行方式: b
服务器通过第二个端口连接到客户端, (二进制)数据类型使用文件数据结构。


  1. 配置数据类型(必需):


    类型I



  2. 数据结构(可选,默认):


    STRU F



  3. 配置转移模式(可选,默认):

    lockquote

    模式S



  4. 选择一个可用端口(这可能比您想象的更微妙)并开始收听。如果您选择默认端口(20),请跳过下一步。



    lockquote

    选择端口,创建套接字, p>


  5. 告诉服务器我们要监听给定的端口(如果选择了默认端口,请注意:


    PORT your-public-ip-address,selected-port



  6. 告诉服务器预期文件传输: STOR远程文件 -

  7. >

    发送文件内容


    打开文件,发送内容,关闭文件


    关闭数据连接


    关闭套接字。




  • 客户端通过第二个端口连接到服务器,并通过im使用文件数据结构的年龄(二进制)数据类型。


    1. 配置数据类型(必需):


      类型I


    2. 配置数据结构可选,默认):


      STRU F



    3. <
      $ b


      模式S



    4. 告诉服务器,我们将监听给定的端口(必需):


      阅读包含IP的 PASV 命令响应

      地址和服务器正在监听的端口号。
    5. 告诉服务器期望文件传输:

      < blockquote>

      STOR远程文件名



    6. 建立连接:


      通过IP地址和端口n连接到服务器从> PASV 响应



    7. 发送文件内容


      打开文件,发送内容,关闭文件


    8. 关闭数据连接


      关闭套接字。



    9. $ b

    第一种方法有一些不方便的问题,因为选择一个端口有点棘手(在使用端口,在再次使用之前需要等待一段时间),并且防火墙或ISP可能会阻止某些端口上的传入连接等。第二种方法最简单,应该是首选的,除非服务器拒绝它。


    I need to get the response messages from an ftp server I'm troubleshooting a connection to, so I am using PHP's ftp_raw function, which allows me to send raw ftp commands to the remote server, and get the response string back. (The built-in PHP ftp commands don't return responses :(

    Following this accepted answer, The command I'm sending is

    PASV
    STOR /local/path/to/file.txt
    

    And the server response is

    500 /local/path/to/file.txt: The system cannot find the path specified.
    

    And I'm thinking to myself "Of course, the remote host has no idea of my local file system." My hunch is that I'm opening a socket, specifying a remote file name, and I still have to pipe the data through. But I haven't found anything conclusive in documentation in my searching.

    What is the complete set of raw ftp commands to upload a file? At what point, and how, do I actually start sending data to the remote server? Can I use the connection set up from ftp_connect() as the socket?

    解决方案

    Disclaimer: The first thing you should know is that RFC959 was written a while after FTP became popular and there is still some broken software based on the lack of specification there was before (and some time after) RFC959 was published. Many older (and more stable) FTP libraries have some special handling for some servers to make sure it works the way you want 99.9% of the time. This is especially more common with handling of extensions to the FTP protocol.

    The rest of my answer assumes the server is RFC959 compliant.

    Also keep in mind that bypassing your FTP client library's higher-level request/response management means you'll need to re-implement a part of this library yourself. This means you should be comfortable with the specification since you'll need to refer to it. Where possible, I'll refer to the appropriate sections so that you can get around.

    In an case, I strongly recommend that you debug your problems by stepping into PHP's FTP client library rather than implementing all of this yourself. It it's possible, you should really request that the library output all the commands it's using. All that being said, I will still guide you through the procedure to help diagnose your problem.


    Managing the FTP data connection is somewhat of a pain. It's not as easy as it looks at first glance if you want to support all optional parts of the specification. Exactly how you transfer files mostly depends on the current state of the following options:

    1. the data type (section 3.1.1): transferring files is usually safest and most efficient using the image/binary data type. This is not the default and some FTP commands (such as directory listings) require setting it to ASCII, so make sure you always set it before a transfer.
    2. the data structure (section 3.1.2): the file structure is usually what you want, but some older computers and mainframes might have to convert to and from this mode.
    3. the transmission mode (section 3.4): the stream mode is most commonly used, but the block mode supports resuming interrupted transfers and compressed mode is of little interest.
    4. the connection mode (sections 3.2 and 3.3): either the client or the server may establish the data connection by connecting to the its peer. This must be negotiated using either:
      • default: the client listens on port 20; or
      • a custom port: the client tells the server it's listing on another port; or
      • passive mode: the client asks on which port the server will listen.

    Pay attention to the specification because some configurations allow you to keep the data connection open while other may require you to close it (e.g. stream mode). If the data connection is already open, then you don't need to reconnect to the server on each transfer.


    All this seems really complicated, but it's just informative. It might come in handy while you're debugging. There are really only two popular ways to transfer files using FTP:

    1. The server connects to the client on a second port and sends the file in the image (binary) data type using the file data structure.

      1. Configure the data type (required):

        TYPE I

      2. Configure the data structure (optional, default):

        STRU F

      3. Configure the transfer mode (optional, default):

        MODE S

      4. Choose an available port (this may be somewhat more subtle than you think) and start listening. If you choose the default port (20), skip the next step.

        choose port, create socket, listen on selected port.

      5. Tell the server we'll be listening on a given port (optional if default port is selected, but it doesn't hurt to be careful):

        PORT your-public-ip-address, selected-port

      6. Tell the server to expect a file transfer:

        STOR remote-file-name

      7. Wait for incoming connection from the server.

      8. Send the file contents

        open file, send contents, close file

      9. Close the data connection

        close socket.

    2. The client connects to the server on a second port and sends the file in the image (binary) data type using the file data structure.

      1. Configure the data type (required):

        TYPE I

      2. Configure the data structure (optional, default):

        STRU F

      3. Configure the transfer mode (optional, default):

        MODE S

      4. Tell the server we'll be listening on a given port (required):

        PASV

      5. Read the PASV command response containing IP address and port number the server is listening on.

      6. Tell the server to expect a file transfer:

        STOR remote-file-name

      7. Establish connection:

        connect to server on IP address and port number from PASV response

      8. Send the file contents

        open file, send contents, close file

      9. Close the data connection

        close socket.

    There are some inconvenient issues with the first method since choosing a port is a little tricky (after using a port, you need to wait for a short period before using it again) and your firewall or ISP may block incoming connections on some ports, etc. The second method is easiest and should be preferred unless the server rejects it.

    这篇关于STOR命令的正确用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆