实现了一个使用TCP相互通信的服务器和客户端程序。
该服务器提供了分配服务部分的语音标签,从客户端接收的句子,并将结果返回给客户端。
该客户端读取存储在一个文本文件,然后发送句子到服务器的英语句子。收到结果后,应打印到标准输出。
代码如下:
server.py
import socket
import sys
import nltk
import ssl
import json
ssl._create_default_https_context = ssl._create_unverified_context
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
# get port numer from command line arguments
portNum = int(sys.argv[1])# should be 55703
# create an INET socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to the host and a port
server_socket.bind(("localhost", portNum))
# Listen for incoming connections from clients
server_socket.listen(10)
# A indefinite loop
while True:
# accept connections from outside
(client_socket, address) = server_socket.accept()
print(("Listening for incoming connections on port ")+str(server_socket.getsockname()[1]))
all_data = ""
while True:
# Read data from client and send it back
data = client_socket.recv(2048)
data = data.decode("utf-8")
#if receive something,continue
if(data != ""):
print(("Client {0!s} is connected.").format(address[0]))
#if receive [END],break
if("[END]" in data):
all_data = all_data + data
break
else:
all_data = all_data + data
else:
print("Connection failed.")
if(len(all_data)):
all_data = all_data.rstrip("[END]")
print(all_data)
tokens = nltk.word_tokenize(all_data)
tagged = nltk.pos_tag(tokens)
json_array = json.dumps(tagged)
sendData = json_array.encode("utf-8")
client_socket.sendall(sendData)
else:
print("No data received.")
# Close the socket
client_socket.close()
print("Client disconnected.")
client.py
import socket
import ssl
import sys
import json
#import os
#size = os.path.getsize('sentence2.txt')
#print(size)
# get arguments from command line
hostName = sys.argv[1]
portNum = int(sys.argv[2])# should be 55703
filePath = sys.argv[3]
# create an INET TCP socket
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to the server (change localhost to an IP address if necessary)
try:
soc.connect(("localhost", portNum))
print("Connected to server.")
# Send messages to the server
with open(filePath) as file:
msg = file.read()
msg = msg+"[END]"
msg = msg.encode("utf-8")
soc.send(msg)
file.close()
except:
print(("Cannot connect to server at {0!s},{0!s}").format(address,portNum))
#raise Exception("Cannot connect to server at {0!s},{0!s}").format(address,portNum)
all_data = ""
while True:
# Receive data from the server
tagged = soc.recv(2048)
string = tagged.decode("utf-8")
if(string == ""):
break
else:
all_data = all_data + string
json_array = json.loads(all_data)
item_str1 = ""
item_str2 = ""
for item in json_array:
if(json_array.index(item) != len(json_array)-1):
item_str1 = item_str1 + item[0] + " "+ ";" + " "
if(json_array.index(item) == len(json_array)-1):
item_str1 = item_str1 + item[0]
for item in json_array:
if(json_array.index(item) != len(json_array)-1):
item_str2 = item_str2 + item[1] + " " + ";" + " "
if(json_array.index(item) == len(json_array)-1):
item_str2 = item_str2 + item[1]
print(item_str1)
print(item_str2)
# Always close the socket after use
soc.close()