1

I am new to python and I am trying to write a python script that can retrieve information (size,etc) of a file which is located on a different application server. I don't know which package I can use to connect to a different app server using it's IP.

Are there any such packages that i can use ?

I tried this connectivity using socket but that does not work, I am not sure if this is correct.

import socket

HOST = ('10.211.36.85','8083')
print type(HOST)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.bind('HOST')
socket.create_connection('10.211.36.85:8083'

s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
       conn.sendall(data)
conn.close()

Please advise if there is any other simpler way. Thankyou for your help and time in advance.

Darshan
  • 117
  • 7
  • Possible answer: http://stackoverflow.com/questions/16662184/checking-file-size-in-a-remote-server-using-python-ssh – kiasy Nov 11 '13 at 23:52

2 Answers2

0
# Echo server program
import socket

HOST = ''                 # Symbolic name meaning the local host
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()

# Echo client program
import socket

HOST = 'daring.cwi.nl'    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

This is per http://docs.python.org/release/2.5.2/lib/socket-example.html

Just write some functions on the server side that checks for a file and then s.send("File found") or whatever you would like.

SJP
  • 1,330
  • 12
  • 21
  • Thankyou so much for your response everyone ... I am a bit confused with socket programming, i cannot write any script on the server side because i run this utility on one of the application servers and this utility is supposed to get details like file size, last date modified and crc of some specific files from each application server. The only challenge i am facing right now is to connect to the other application server's and get this information about those files. Please suggest what would be best way to do this ? – Darshan Nov 12 '13 at 16:02
0

You appear to have a good start there, and some good suggestions as well.

You'll probably want to create a miniature command interpreter, one command for file length, and another command for each of the other things you need to be able to do. A big if/elif/elif/.../else right after your main socket.recv() should do it.

However, when you write a socket program, you leave the possibility of subtle errors cropping up if you assume a one to one relationship between your socket.send()'s and your socket.recv()'s. TCP sockets reserve the right to aggregate multiple socket.send()'s into one packet, or to split a packet into multiple recv's, and about any other size alteration it can think of for performance or reliability.

For this reason, I like to use my bufsock.py module, to make the length rearrangements a nonissue, and simply retrieving data at the same time: http://stromberg.dnsalias.org/~dstromberg/bufsock.html

BTW, depending on what kind of info you need, it might be easier to just sshfs or CIFS or NFS the filesystem, and access it as though it's local.

Anyway, in case you want or need to use sockets, here's an example in Python 3. It's based on SJP's post:

#!/usr/bin/python3

import os
import sys
import time
import socket as socket_mod

sys.path.insert(0, '/usr/local/lib')
import bufsock as bufsock_mod

if sys.argv[1:] and sys.argv[1] == 'server':
    # Echo server program
    HOST = 'localhost'        # Symbolic name meaning the local host
    PORT = 50007              # Arbitrary non-privileged port
    socket = socket_mod.socket(socket_mod.AF_INET, socket_mod.SOCK_STREAM)
    socket.bind((HOST, PORT))
    socket.listen(1)
    connection, address = socket.accept()
    print('Connected to from {}'.format(address))
    bufsock = bufsock_mod.bufsock(connection)
    while True:
        print('Reading command')
        sys.stdout.flush()
        command = bufsock.readto(b'\0')[:-1]
        print('Got {} command'.format(command))
        sys.stdout.flush()
        if command == b'stat':
            filename = bufsock.readto(b'\0')[:-1]
            length = os.stat(filename).st_size
            length_bytes = bytes(str(length), 'ISO-8859-1')
            bufsock.send(length_bytes + b'\0')
            bufsock.flush()
        elif command == b'date':
            bufsock.send(bytes(time.ctime(), 'ISO-8859-1') + b'\0')
            bufsock.flush()
        elif command == b'exit':
            sys.stderr.write('{}: terminating on exit command\n'.format(sys.argv[0], ))
            bufsock.close()
            sys.exit(0)
        else:
            sys.stderr.write('{}: Illegal command received: {}\n'.format(sys.argv[0], command))
            bufsock.close()
            sys.exit(1)
elif sys.argv[1:] and sys.argv[1] == 'client':
    # Echo client program
    HOST = 'localhost'        # The remote host
    PORT = 50007              # The same port as used by the server
    socket = socket_mod.socket(socket_mod.AF_INET, socket_mod.SOCK_STREAM)
    socket.connect((HOST, PORT))
    bufsock = bufsock_mod.bufsock(socket)

    # Get the length of /etc/services
    bufsock.send(b'stat\0/etc/services\0')
    bufsock.flush()
    length = bufsock.readto(b'\0')
    print('length of /etc/services is {}'.format(length))

    bufsock.send(b'date\0')
    bufsock.flush()
    date = bufsock.readto(b'\0')
    print('date is {}'.format(date))

    bufsock.send(b'exit\0')

    bufsock.close()
else:
    sys.stderr.write('{}: Must call with "server" or "client" argument\n'.format(sys.argv[0]))
    sys.exit(1)

HTH :)

dstromberg
  • 6,954
  • 1
  • 26
  • 27
  • Thankyou so much for your response everyone ... I am a bit confused with socket programming, i cannot write any script on the server side because i run this utility on one of the application servers and this utility is supposed to get details like file size, last date modified and crc of some specific files from each application server. The only challenge i am facing right now is to connect to the other application server's and get this information about those files. Please suggest what would be best way to do this ? – Darshan Nov 12 '13 at 18:07
  • What OS's are involved? It sounds like you may be best served by a network filesystem like sshfs, CIFS or NFS. Then you can "see" the remote files as though they are local. – dstromberg Nov 12 '13 at 21:09
  • We are working on Linux. all application servers would be on linux, – Darshan Nov 14 '13 at 20:27
  • sshfs might be easier than writing a socket program. – dstromberg Nov 15 '13 at 03:33