Off topic:WindowsError: [Error 2] The system cannot find the file specified - python 2.7
0
0
Entering edit mode
9.0 years ago
ezis98 • 0

Hey

I'm working on a server-client assignment in python.

The client can send 5 types of requests to the server:

  1. get the server's IP
  2. get contents of a directory on the server
  3. run cmd command on the server and get the output
  4. open a calculator on the server
  5. disconnect

My server manages to respond to all request, except for type-3 requests.

This is the error I get when I try to run a cmd command on the server code (line 32 on server):

WindowsError: [Error 2] The system cannot find the file specified

server code:

__author__ = 'eyal'

from struct import pack, unpack, calcsize
import socket
from os import listdir
from subprocess import check_output, call


def server():
    ser_soc = socket.socket()
    ser_soc.bind(("0.0.0.0", 8080))
    ser_soc.listen(1)
    while True:
        accept_flag = raw_input("Would you like to wait for a client? (y/n) ")
        if accept_flag == "y":
            client_soc, client_address = ser_soc.accept()
            while True:
                client_structs = client_soc.recv(1024)
                msg_type, data_len = unpack(">BH", client_structs[:calcsize(">BH")])
                if data_len > 0:
                    msg_data = unpack(str(data_len) + "s", client_structs[calcsize(">BH"):])
                if msg_type == 1:
                    ip = socket.gethostbyname(socket.gethostname())
                    to_send = pack(">BH" + str(len(ip)) + "s", msg_type, len(ip), ip)
                elif msg_type == 2:
                    content = listdir(msg_data[0])
                    content_str = "\r\n".join(content)
                    to_send = pack(">BH" + str(len(content_str)) + "s", msg_type,
                                   len(content_str), content_str)
                elif msg_type == 3:
                    command = str(msg_data).split()
                    output = check_output(command) #line32
                    to_send = pack(">BH" + str(len(output)) + "s", msg_type, len(output), output)
                elif msg_type == 4:
                    call("calc")
                    to_send = pack(">BH" + str(len("The calculator is open.")) + "s", msg_type,
                                   len("The calculator is open."), "The calculator is open.")
                elif msg_type == 5:
                    client_soc.close()
                    print "The client has disconnected."
                    break
                else:
                    to_send = pack(">BH" + str(len("invalid message type, try again")) + "s",
                                   msg_type, len("invalid message type, try again"),
                                   "invalid message type, try again")
                if msg_type != 5:
                    client_soc.send(to_send)
        else:
            break
    ser_soc.close()


def main():
    server()

if __name__ == "__main__":
    main()

client code:

__author__ = 'eyal'


from struct import pack, unpack, calcsize
import socket


def client():
    my_soc = socket.socket()
    my_soc.connect(("127.0.0.1", 8080))
    while True:
        send_flag = raw_input("Would you like to send the server a request? (y/n) ")
        if send_flag == "y":
            msg_code = input("What type of request would you like to send?\n"
                             "1. Get the server's IP address.\n"
                             "2. Get content of a directory on the server.\n"
                             "3. Run a cmd command on the server and get the output.\n"
                             "4. Open a calculator on the server.\n"
                             "5. Disconnect from the server.\n"
                             "Your choice: ")
            if msg_code == 1 or msg_code == 4 or msg_code == 5:
                to_send = pack(">BH", msg_code, 0)
            elif msg_code == 2:
                path = raw_input("Enter path of wanted directory to get content of: ")
                to_send = pack(">BH" + str(len(path)) + "s", msg_code, len(path), path)
            elif msg_code == 3:
                command = raw_input("Enter the wanted cmd command, including arguments: ")
                to_send = pack(">BH" + str(len(command)) + "s", msg_code, len(command), command)
            else:
                print **"Invalid message code, try again\n"

            if 1 <= msg_code <= 5:
                my_soc.send(to_send)
            if 1 <= msg_code < 5:
                data = my_soc.recv(1024)
                msg_code, msg_len = unpack(">BH", data[:calcsize(">BH")])
                msg_data = unpack(str(msg_len) + "s", data[calcsize(">BH"):])
                print "The server's response to your type-" + str(msg_code) + " request:"
                print msg_data[0]
            if msg_code == 5:
                print "You have disconnected from the server."
                break
        else:
            break
    my_soc.close()


def main():
    client()


if __name__ == "__main__":
    main()

EDIT: Added position marker at line 32

python • 9.2k views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 4034 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6