简体   繁体   中英

Why does this Python Function create a Unix Executable?

I am creating a primitive socket server to teach myself more about networking. In my attempt to create it, I have decided to try to create a "chat log." My plan is to have the server backend script (the script that powers the Tkinter GUI) append all sent messages to a text file called old_msg.txt .

To do this, I created a function called old_msg . It is defined here.

def old_msg(data):
    while True:
        oldmsg = open("old_msg.txt", "a+")
        try:
            oldmsg.write("%s \n" % (data))
        except TypeError:
            print "SERVER MSG >>> NO MESSAGE SENT"
        oldmsg.close()

I call it in another function, clientthread , which appends the data if it is not empty.

while True:

    #Receiving from client
    data = connection.recv(1024)
    if not data:
        break
    if data:
        connection.sendall(data)
        old_msg(data)
    print data

connection.close()

Unusually, this code creates a UNIX_EXECUTABLE. It is called old_msg , and here is some of the text that is shown in the shell. This is repeated thousands of times.

/Volumes/***'s USB/Python Programs/Sockets/IM Project/Server/Different Idea/old_msg: line 43159: hello: command not found
/Volumes/***'S USB/Python Programs/Sockets/IM Project/Server/Different Idea/old_msg: line 43160: hello: command not found
logout

Why would this code create a unix executable?

EDIT

I sent the message "Hello World" to the server. You can find the pastebin here

This code writes a text file old_msg.txt containing the message, hello world . This text file is written on a USB disk or stick using a non-UNIX filesystem such as FAT32. Therefore, the text file has the execute bit set by default. This is because FAT32 does not have an execute bit and the UNIX permissions shown and used by Mac OS X are made up by the operating system. Also, Mac OS X seems to be hiding the extension .txt from you (I believe that is tunable in Finder's settings). So you get what appears to be an executable named old_msg . When you click on that to run it, the OS attempts to interpret it as a script, which results in the error messages you are seeing; the first word of your message hello world is interpreted as the file name of the UNIX command to run.

This code doesn't create a unix executable. But somehow, you have set the file's executable bit to true. Your code doesn't do this, though.

This is a partial answer. I am open to other ones - this is simply something that I discovered.

I believe that the issue with my code was the while True line in the old_msg(data) variable. When I got rid of that, the script/exec/whatever-it-was was not created.

The issue was the double loop which comes from the old_msg() function and the other loop within the clientthread function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM