I'm trying to split string if even or odd for example:
"Tea" will return two strings string1 = "Te" and string2 = "a" - odd
"Coffee" will return two strings string1 = "Cof" and string2 = "fee"
I've got a working algorithm that does that.
Then client needs to send this to the server. My question how to send this to the server. How server accepts these two strings?
I'm newbie to python. Please help.
Server side:
import socket
def Main():
host = '127.0.0.1'
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
print("Server Started")
while True:
data, addr = s.recvfrom(1024)
data = data.decode('utf-8')
print("Message From: " +str(addr))
print("From connected user: " + data)
data = data.upper()
print("Sending: " + data)
s.sendto(data.encode('utf-8'), addr)
s.close()
if __name__ == '__main__':
Main()
Client side:
import socket
def Main():
host = '127.0.0.1'
port = 5001
server = ('127.0.0.1', 5000)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
message = input('Please type a word ')
while message != '':
def splitWord(w):
split = -((-len(w))//2)
s1 = (w[:split])
s2 = (w[split:])
print(s1)
print(s2)
return
a = splitWord(message)
s.sendto(a.encode('utf-8'), server)
data, addr = s.recvfrom(1024)
data = data.decode('utf-8')
print("Received from server: " + data)
message = input('-> ')
s.close()
if __name__ == '__main__':
Main()
You might want to revisit your splitWord()
function.
Inside splitWord()
you create two substrings: s1 and s2, but don't do anything with them (other than printing). Your return is empty, and you don't modify the argument either.
A better way of to define this function is:
def splitWord(w):
return w[:len(w)/2], w[len(w)/2:]
Example:
a = "Coffee"
f,s = splitWord(a)
print f, s
-> Cof fee
Also there is no reason to define splitWord()
inside the while loop.
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.