0

I am trying to make a TCP port server in python. Here is my code so far:

import socket 

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
sock.bind(('',4000)) 
sock.listen(1) 

while 1: 
    client, address = sock.accept() 
    fileexists = client.RUNCOMMAND(does the file exist?)

    if fileexists = 0:
           client.close()
    else if: 
        filedata = client.RUNCOMMAND(get the contents of the file)

        if filedata = "abcdefgh":
              client.send('Transfer file accepted.')
        else:
              client.send('Whoops, seems like you have a corrupted file!')

    client.close()

I just have no idea how to run a command (RUNCOMMMAND) that would check if a file exists on the client. Also, is there a way to check what operating system the client is on to run different commands (eg. linux will have a file finder different command than windows). And I totally understand if this isn't possible, but I am really hoping that there is a way to do this.

Thank you very much.

2
  • This looks like code for a server, unless you are doing a P2P thing. What code is running on the client and making the connection to the server? Only that machine can know if a file exists, so the server will have to send a request for the file, and the "file exists?" check will be done on the client machine running its own client code. Commented Mar 20, 2013 at 3:40
  • @Paul The client code is basic python that connects to port 4000 and sends data. The action that I want here is to have a ssh-like file checking system. If I have the operation done client-side with a response "Yes, I have the file" then anyone could connect to the server and type "Yes, I have the file" to get in. This is, effectively, a password to access the server. This is a problem, and I want the brute-force attempts completely sealed off by the server checking if the file exists. If the file does not exist, the server will deny instead of allowing for the chance to enter a password. Commented Mar 20, 2013 at 4:47

2 Answers 2

1

XMLRPC may help you. XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. http://docs.python.org/2/library/xmlrpclib.html

Sign up to request clarification or add additional context in comments.

Comments

0

You might want to look at the very handy bottle.py micro server. its great for small server tasks like this and you get the Http protocol on top of this. You just include one file with your code. http://bottlepy.org

here is code that will work from http://blah:8090/get/file or http://blah:8090/exists/file so to see the contents of /etc/hosts would be http://blah:8090/get/etc/hosts

#!/usr/bin/python
import bottle 
import os.path


@bottle.route("/get/<filepath:path>")
def index(filepath):
    filepath = "/" + filepath
    print "getting", filepath 
    if not os.path.exists(filepath):
        return "file not found"

    print open(filepath).read() # prints file 
    return '<br>'.join(open(filepath).read().split("\n")) # prints file with <br> for browser readability

@bottle.route("/exists/<filepath:path>")
def test(filepath):
    filepath = "/" + filepath
    return str(os.path.exists(filepath))


bottle.run(host='0.0.0.0', port=8090, reloader=True)

the reloader option on the run method allows you to edit the code without manually restarting the server. Its quite handy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.