01 February, 2011

a simple ftp program with python xmlrpc

Hope you have read my post about creating a simple client server program with python xmlrpc. Now we will  try creating a sample file transfer program through which we can transfer files (text, images, movie) over the network. The logic behind this is the same that we used in the client server program shown in the above link. The remote procedure call allows to call functions placed in a remote server, passing parameters to it and receiving return values from the remote function in the server. 

1. Sending a file to remote server 

       Inorder to send a file, say a picture file we first open the file and read it's contents. Then we call the remote function in the server with file content (of the picture) as parameter. The remote function on receiving this contents as parameters will open a new file and write it into that file...

Check the ftp client and server program below...

ftp client


   import xmlrpclib
   server = xmlrpclib.ServerProxy('http://127.0.0.1:9009')
   filepath=raw_input('Enter path to file :')
   try:
         with open(filepath, "rb") as handle:
            data=xmlrpclib.Binary(handle.read())
            handle.close()
            a=server.UploadFile(data
   except:
         print "Upload failed"



 ftp server :
 

   import xmlrpclib
   from SimpleXMLRPCServer import SimpleXMLRPCServer 
   def UploadFile(filedata):
        try:
            fp="/home/jo/Downloads/file2"
            with open(fp, "wb") as handle:
                    data1=filedata.data
                    handle.write(data1)
                    handle.close()
            return True
        except Exception,ex:
            return 'error'
   client.register_function(UploadFile)
   server.serve_forever()


2. Receiving a file from remote server 

         Here we call the remote function on the server with file path as the parameter, where file path is the path to the file that is going to be downloaded to the client. Server on receiving the file path, opens file and returns it's contents to the client which called the remote function. Client will open a new file and writes the  above returned value to the file.

ftp server :


     import xmlrpclib
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    def movefile(filepath):
        try:
            handle = open(filepath)
            return xmlrpclib.Binary(handle.read())
            handle.close()
        except:
            return 'error'
    client.register_function(movefile)
    server.serve_forever()


ftp client :

   import xmlrpclib
   server = xmlrpclib.ServerProxy('http://127.0.0.1:9009')
   filepath=raw_input('enter path : ')
   try:
         handle=open("/home/jo/Downloads/file1","w")
         handle.write(server.movefile(filepath).data)
         handle.close()
    except:
         print 'Download failed'



remarks ::

In the above examples the data read from the file is converted to binary format before transferring it using the Binary() function in the xmlrpc library. Also you have to change the ip to desired on replacing 127.0.0.1 to run the program on a network.        


Read rest of entry

26 April, 2010

Internet access and package installation behind a proxy server in Ubuntu

When there is a proxy server in between your system and server you have to specify the proxy address in all the applications that is intended to access internet such as firefox, bittorrent... Proxy servers are mainly deployed for subnets, for example a network inside a college is connected to internet using a proxy server (not technically, since there are gateways,routers...) to sensor and restrict the students internet activities, protect from unauthorized access... 

If you have to run any application behind a proxy server, you have to specify the proxy server address and the port number to the application that need access the internet. This is because the default port numbers used by the transport  layer protocols are changed by the proxy server. Squid is one among the important proxy programs that is prefered by network administrators. In the following cases please replace the proxy address and port number with the corresponding feilds applicable to you.

Setting Proxy address in Mozilla  firefox

Click on edit --> Preferences.

Select the Network tab under the advanced tab. Click on the Settings button. In the Network settings window select the manual configuration. Then enter the HTTP proxy and port on the current window. Save the settings...When you start browsing the firefox will automatically ask for authentication. Then you have to specify the username and password in the proxy server. 






Setting Proxy in Synaptic package manager


If you are using synaptic package manager as the package installer, You have to do some configuration before using apt package manager.
Click on the Settings --> Preferences
Choose the network tab and select manual proxy configuration. Set HTTP proxy and FTP proxy. Then enter their port numbers also. Click on the authentication button to set username and password.




Setting proxy for APT package manager
APT is a command line utility for installing packages. Probably many people are addicted to terminal do the following . . .

If you have no authentication for the proxy server then use the following command line. 

export http_proxy=http://192.168.0.2:3128
export ftp_proxy=http://192.168.0.2:3128


If you have username and password the use

export http_proxy=http://username:password@192.168.0.2:3128/
export ftp_proxy=http://username:password@192.168.0.2:3128/
Read rest of entry
 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger