Last active
October 31, 2019 00:42
-
-
Save pratikpc/34d3a48191520efb122d9e6549ed0fae to your computer and use it in GitHub Desktop.
TCP Client & Server using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ToStr(string): | |
return str(string)[2:-1] | |
import socket | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client: | |
client.connect(('localhost', 9000)) | |
string = 'ABCBA' | |
client.sendall(bytes(string, 'utf-8')) | |
palindrome = ToStr(client.recv(1024)) | |
if palindrome == 'true': | |
print(string, "is palindrome") | |
else: | |
print(string, "is not palindrome") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
def ToStr(string): | |
return str(string)[2:-1] | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server: | |
server.bind(('localhost', 9000)) | |
server.listen() | |
while True: | |
conn, addr = server.accept() | |
with conn: | |
string = ToStr(conn.recv(1024)) | |
reversed = string[::-1] | |
print(string, reversed) | |
if reversed == string: | |
conn.send(b'true') | |
else: | |
conn.send(b'false') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment