如何更改命令行以执行
How to change command line for execution
我为聊天客户端和聊天服务器编写了一个简单的代码(一个单独的代码用于客户端,一个用于服务器)。我的服务器当前使用命令行执行
python chatserve.py <port number>
刚发现class要求程序以如下命令行启动:
./chatclient <portnumber>
如何转换?我将不胜感激 tips/help。谢谢!
(为了澄清任何混淆,我的聊天客户端的执行也需要在其命令行中使用 ./chatclient
,但是由于那部分代码是用 C 语言编写的,所以我能够弄清楚如何让它用特定的命令行执行。不幸的是,我对 Python 不太熟悉。)
代码如下:
#!/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()
按照以下步骤操作:
- 将文件名重命名为:
chatserve
- 在代码的第一行添加以下命令:
#!/usr/bin/python
或 #!/usr/bin/python2
或 #!/usr/bin/python3
- 给他执行权限:
chmod +x chatserve
我为聊天客户端和聊天服务器编写了一个简单的代码(一个单独的代码用于客户端,一个用于服务器)。我的服务器当前使用命令行执行
python chatserve.py <port number>
刚发现class要求程序以如下命令行启动:
./chatclient <portnumber>
如何转换?我将不胜感激 tips/help。谢谢!
(为了澄清任何混淆,我的聊天客户端的执行也需要在其命令行中使用 ./chatclient
,但是由于那部分代码是用 C 语言编写的,所以我能够弄清楚如何让它用特定的命令行执行。不幸的是,我对 Python 不太熟悉。)
代码如下:
#!/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()
按照以下步骤操作:
- 将文件名重命名为:
chatserve
- 在代码的第一行添加以下命令:
#!/usr/bin/python
或#!/usr/bin/python2
或#!/usr/bin/python3
- 给他执行权限:
chmod +x chatserve