Skip to content

Instantly share code, notes, and snippets.

@pratikpc
Last active October 31, 2019 00:42
Show Gist options
  • Save pratikpc/34d3a48191520efb122d9e6549ed0fae to your computer and use it in GitHub Desktop.
Save pratikpc/34d3a48191520efb122d9e6549ed0fae to your computer and use it in GitHub Desktop.
TCP Client & Server using Python
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")
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