Add simple client
This commit is contained in:
parent
e9232c8905
commit
36bf7d3c29
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import logging
|
||||
import socket
|
||||
import sys
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("host", help="The IPv6 address of the host to connect to.")
|
||||
parser.add_argument("--port", "-p", default=50007, help="The port to connect to.")
|
||||
args = parser.parse_args()
|
||||
|
||||
s = None
|
||||
for res in socket.getaddrinfo(args.host, args.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
|
||||
af, socktype, proto, canonname, sa = res
|
||||
try:
|
||||
s = socket.socket(af, socktype, proto)
|
||||
except OSError as msg:
|
||||
s = None
|
||||
continue
|
||||
try:
|
||||
s.connect(sa)
|
||||
except OSError as msg:
|
||||
s.close()
|
||||
s = None
|
||||
continue
|
||||
break
|
||||
if s is None:
|
||||
print('could not open socket')
|
||||
sys.exit(1)
|
||||
with s:
|
||||
s.sendall(b'Hello, world')
|
||||
data = s.recv(1024)
|
||||
print('Received', repr(data))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue