0

I wrote a simple code for a chat client and chat server (a separate code for the client and one for the server). My server currently executes with the command line

python chatserve.py <port number>

Just found out that class requirement is that the program starts with the following command line:

./chatclient <portnumber>

How do I convert? I would greatly appreciate any tips/help. Thank you!

(To clarify any confusion, the execution of my chat client also needs a ./chatclient in its command line, but since that part of the code was written in C, I was able to figure out how to get it to execute with specific command lines. I'm not as familiar with Python unfortunately.)

Here is the code:

#!/bin/python

from socket import *
import sys

#initiate chat with client
def chat(connectionsocket, clientname, username):
    to_send = ""
    while 1:    # continue chat until break
        # get characters from the user
        received = connectionsocket.recv(501)[0:-1]

        # if we received nothing, print close message and break
        if received == "":
            print "Closed connection.  Wait for new connection..."
            break

        # print client username and message
        print "{}> {}".format(clientname, received)

        # get server input and send to client
        to_send = ""
        while len(to_send) == 0 or len(to_send) > 500:
            to_send = raw_input("{}> ".format(username))

        # special "\quit" message 
        if to_send == "\quit":
            print "Closed connection.  Wait for new connection..."
            break
        connectionsocket.send(to_send)

#initiate handshake with client
def handshake(connectionsocket, username):
    # get the client username
    clientname = connectionsocket.recv(1024)

    # send server username to the client
    connectionsocket.send(username)

    return clientname

#execution
if __name__ == "__main__":

    # If wrong number of arguments, print error message and exit
    if len(sys.argv) != 2:
        print "Error: no port number input"
        exit(1)

    # get port number and create TCP socket
    serverport = sys.argv[1]
    serversocket = socket(AF_INET, SOCK_STREAM)

    # bind socket to port
    serversocket.bind(('', int(serverport)))

    # listen on port for incoming messages
    serversocket.listen(1)

    # get username
    username = ""
    while len(username) == 0 or len(username) > 10:
        username = raw_input("Enter username (10 characters or less): ")
        print "Receiving incoming messages..."

    # continue receiving incoming messages until close
    while 1:
        # create new socket for incoming connection
        connectionsocket, address = serversocket.accept()

        # print connection message 
        print "Receiving connection on address {}".format(address)

        # initiate handshake and chat with incoming connection
        chat(connectionsocket, handshake(connectionsocket, username),     username)

        # close connection
        connectionsocket.close()
Aisha Ashwal
  • 83
  • 3
  • 11

1 Answers1

1

Follow these steps:

  1. Rename the filename to: chatserve
  2. Add the following command in the first line of your code: #!/usr/bin/python or #!/usr/bin/python2 or #!/usr/bin/python3
  3. Give him permission to execute: chmod +x chatserve
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I think I followed your instructions - basically removed the `.py` from my `chatserve.py` file to rename it `chatserve`. It already had `#!/usr/bin/python` as my first line so I didn't change anything there. Then granted permission to execute. Then I tried to run `./chatclient but that did not quite work unfortunately. Am I doing something wrong? – Aisha Ashwal May 01 '17 at 03:53
  • I forgot to add, the error message I got was `./chatserve: Command not found.` – Aisha Ashwal May 01 '17 at 04:00
  • Are you located in the same folder as the chatserve file? – eyllanesc May 01 '17 at 04:04
  • Yes. Sorry, I meant to say the error message I got was `Error: Invalid number of arguments` when I tried to execute with `./chatclient `. When I tried to execute with `./chatserve ` (which is not really what I want to do), I get the `command not found` message. – Aisha Ashwal May 01 '17 at 04:06
  • That error is already in your code, if you share your code it could help you. – eyllanesc May 01 '17 at 04:08
  • Ah, I actually managed to execute the program with `python ./chatserve `. But is there a way to execute with just `./chatserve`? Kind of like how in C or C++, you first compile the program with `gcc...-o chatserve ...` and then just execute with `.chatserve...`? – Aisha Ashwal May 01 '17 at 04:14
  • Does not run `.chatserve` but `./chatserve`. With what you put should work, but more information of your code you could not help – eyllanesc May 01 '17 at 04:17
  • To get the parameter `portnumber` you must use: `import sys; portnumber = int (sys.argv[1])`. Verify that your code is like this – eyllanesc May 01 '17 at 04:19
  • You shouldn't need to use sudo on step 3 – hugos May 01 '17 at 04:31
  • I edited to include the code. I was able to execute with portnumber before. The problem is that I am supposed to execute with just `./chatserve ` instead of `python ./chatserve ` and I'm not sure how to lose the "python" part. – Aisha Ashwal May 01 '17 at 04:37
  • @AishaAshwal what do you get when you run `which python` in the terminal? – hugos May 01 '17 at 04:48
  • @HugoSadok here is what I get, not sure what I'm doing wrong. https://gyazo.com/c78ac535aa5f1abd377b427893db71f8 – Aisha Ashwal May 01 '17 at 05:03
  • @eyllanesc I'm still getting the same problem, not sure what I'm doing differently. https://gyazo.com/c78ac535aa5f1abd377b427893db71f8 – Aisha Ashwal May 01 '17 at 05:03
  • @AishaAshwal I was asking about the output of running the command `which python` alone. It shows you where the python you're using is installed. – hugos May 01 '17 at 05:07
  • By the way, I recommend you change the first line in your code to `#!/usr/bin/env python` it's portable and usually works. – hugos May 01 '17 at 05:12
  • @HugoSadok Thank you, I changed the first line. Here is the python command output: https://gyazo.com/f872cef4c480179e8a097c9b80469a36 – Aisha Ashwal May 01 '17 at 05:26
  • @HugoSadok Still getting the same message unfortunately https://gyazo.com/7b2e3034925e65cf433cc7b7215cdb10 – Aisha Ashwal May 01 '17 at 06:02
  • @HugoSadok I figured the problem out by looking up this post: https://stackoverflow.com/questions/4993621/how-to-run-python-script-without-typing-python regarding converting CRLF to LF, but you were right that `#~/usr/bin/env python` did the trick. Thank you! – Aisha Ashwal May 01 '17 at 07:18
  • @AishaAshwal Nice to hear, I'm glad it helped you – hugos May 01 '17 at 18:36