###### tags: `python` `TCPclient` `TCPserver`
# TCPclient/TCPserver
```python=
import socket
import argparse
import time
parser = argparse.ArgumentParser(
description="Meohw hecker client programs",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="example\n"
"python tcpClient.py -i meowhecker.com -p 6669 "
)
# User interface
parser.add_argument("-i", "--RHOST", help="input the server name", default="127.0.0.1", required=True)
parser.add_argument("-p", "--RPORT", type=int, help="input the server ports", default="6669", required=True)
group = parser.add_mutually_exclusive_group()
group.add_argument("-q", "--quite", help="don't display ditail information", action="store_true")
group.add_argument("-v", "--verbose", help="show detial information", action="store_true")
args= parser.parse_args()
def main():
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((args.RHOST,args.RPORT))
client.send(bytes("fwer564 ","utf-8"))
'''
message = client.recv(1024)
print(message.decode("utf-8"))
'''
fullMessage=""
while 1:
message = client.recv(8) # flow control (The receiver tells the sender how many bytes he could send) 1byte = char
if len(message)<=0:
break
else:
fullMessage += message.decode("utf-8")
print(fullMessage)
if __name__ == "__main__":
main()
connectFlag=1
#group
if args.verbose and connectFlag == 1:
x=0
while 1:
x+=1
print(f"Throught time = {x}")
time.sleep(1)
```
## Textwarp module
[Reference web](https://docs.python.org/3/library/textwrap.html)
> It's convenient module to print text
### textwarp.dedent('''text''')
>Remove any leading whitespace in every line in text.
## TCP server
```python=
import socket
import threading
import argparse
import textwrap
parser=argparse.ArgumentParser(
description="TCP meowhecer sever ",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="example= tcpServer.py -i meowhecker.com -r 6669\n"
)
parser.add_argument("-i", "--LHOST", help="set self host address !!", default="meowhecker.com",required=True)
parser.add_argument("-p", "--LPORT", type=int, help="set self host Port !!", default=6669, required=True)
args = parser.parse_args()
def clientHandler(socketClient): # job 1
with socketClient as sockets:
password = sockets.recv(1024)
print(f'Receive from clientHost, password is {password.decode("utf-8")}')
socketClient.send(bytes( #Due to TCP is stream-oriented, we need to use byts() or b'' to send and incoding
textwrap.dedent('''
welcome to meowhecer TCP server!!!!!!!!!
meow meow meow meow~~
ServerACK'''),
"utf-8"
)) #send (bytes("messages","encoding"))
socketClient.close() # if you don't close, it will occur problems but I don't know why XD oh noooooooo!
def server(LHOST,LPORT):
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((LHOST,LPORT))
server.listen(10) # queue
print(f'[*] listening on {LHOST}:{LPORT}')
while 1:
socketClient, address = server.accept()
print(f'[*] Connection form {address[0]}:{address[1]} has been edstablished !!!!')
thread = threading.Thread(target=clientHandler,args=(socketClient,))
thread.start()
thread.join() #無聊亂寫 haaaaa (Berfore subthreading job has been all down, the following program will wait for it.
if __name__=="__main__":
server(args.LHOST,args.LPORT)
```