FW: upload file with ftp

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

FW: upload file with ftp

Kees Bleijenberg
I want to upload a file with ftp using library Network.FTP.Client.
The OS is Windows 64 and ghc version is 8.6.4.

This is the code:

import System.FilePath.Windows
import Network.FTP.Client
import qualified Data.ByteString as B
....
testFtp :: IO (Either String ())
testFtp = do
   let host = "copecco"  -- this is from my hosts file      
       username = ****
       pwd = ****
       ftpDir = "registratie"
       fileToSend = "globals.pas"  -- a test text file
   putStrLn $ "Connect to " ++ host
   withFTP host 21 $ \h ftpResponse -> do
         print ftpResponse
         if frStatus ftpResponse == Success
         then do
                  putStrLn "Connected"
                  loginResp <- login h username pwd
                  print loginResp
                  if frStatus loginResp == Success
                  then do
                           putStrLn $ "Change directory to " ++ ftpDir
                           cwdResp <- cwd h ftpDir
                           print cwdResp
                           if frStatus cwdResp == Success
                           then do  
                                    putStrLn "Read file from disk"
                                    let ftpFn =  takeFileName fileToSend
                                    fileContents <- B.readFile fileToSend
                                    putStrLn $ "File size: " ++ show
(B.length fileContents) ++ " bytes"
                                    stor h ftpFn fileContents TI
                                    return $ Right ()
                           else return $ Left $ "Ftp error cwd. Code: " ++
show (frCode ftpResponse)
                  else return $ Left $ "Ftp error login. Code: " ++ show
(frCode ftpResponse)
         else return $ Left $ "Connect to host " ++ host  ++ " failed. Code:
" ++ show (frCode ftpResponse)

This is the response:
Connect to copecco

220 (vsFTPd 3.0.2)

Connected

230 Login successful.

Change directory to registratie

250 Directory successfully changed.

Read file

File size: 27015 bytes

*** Exception: Network.Socket.connect: <socket: 444>: failed (Connection
timed out (WSAETIMEDOUT))  

Everything works fine until the stor commmand.
When I upload the same file with another ftp client program everything works
(no permission problems).

What is wrong and how do I get the result codes for the stor command?

Kees


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: FW: upload file with ftp

Brandon Allbery
At a guess, you're using a modern FTP client that defaults to passive mode for compatibility with firewalls, and you'll need to check the docs to see how to do passive mode with Network.Client.FTP.

On Tue, Jan 5, 2021 at 7:24 AM Kees Bleijenberg <[hidden email]> wrote:
I want to upload a file with ftp using library Network.FTP.Client.
The OS is Windows 64 and ghc version is 8.6.4.

This is the code:

import System.FilePath.Windows
import Network.FTP.Client
import qualified Data.ByteString as B
....
testFtp :: IO (Either String ())
testFtp = do
   let host = "copecco"  -- this is from my hosts file       
       username = ****
       pwd = ****
       ftpDir = "registratie"
       fileToSend = "globals.pas"  -- a test text file
   putStrLn $ "Connect to " ++ host
   withFTP host 21 $ \h ftpResponse -> do
         print ftpResponse
         if frStatus ftpResponse == Success
         then do
                  putStrLn "Connected"
                  loginResp <- login h username pwd
                  print loginResp
                  if frStatus loginResp == Success
                  then do
                           putStrLn $ "Change directory to " ++ ftpDir
                           cwdResp <- cwd h ftpDir
                           print cwdResp
                           if frStatus cwdResp == Success
                           then do 
                                    putStrLn "Read file from disk"
                                    let ftpFn =  takeFileName fileToSend
                                    fileContents <- B.readFile fileToSend
                                    putStrLn $ "File size: " ++ show
(B.length fileContents) ++ " bytes"
                                    stor h ftpFn fileContents TI
                                    return $ Right ()
                           else return $ Left $ "Ftp error cwd. Code: " ++
show (frCode ftpResponse)
                  else return $ Left $ "Ftp error login. Code: " ++ show
(frCode ftpResponse)
         else return $ Left $ "Connect to host " ++ host  ++ " failed. Code:
" ++ show (frCode ftpResponse)

This is the response:
Connect to copecco

220 (vsFTPd 3.0.2)

Connected

230 Login successful.

Change directory to registratie

250 Directory successfully changed.

Read file

File size: 27015 bytes

*** Exception: Network.Socket.connect: <socket: 444>: failed (Connection
timed out (WSAETIMEDOUT)) 

Everything works fine until the stor commmand.
When I upload the same file with another ftp client program everything works
(no permission problems).

What is wrong and how do I get the result codes for the stor command?

Kees


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


--
brandon s allbery kf8nh

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.