-
Notifications
You must be signed in to change notification settings - Fork 0
/
client2.py
51 lines (41 loc) · 1.1 KB
/
client2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-04-10 23:03:37
# @copyright by hoojo@2018
# @changelog Added python3 `socket -> client2` example
# Echo client program
import socket
import sys
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
print('res', res)
af, socktype, proto, canonname, sa = res
print('AddressFamily:', af)
print('sockType:', socktype)
print('proto:', proto)
print('canonname', canonname)
print('host/port:', sa)
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
s = None
continue
try:
s.connect(sa)
except socket.error as msg:
s.close()
s = None
continue
break
if s is None:
print('could not open socket')
sys.exit(1)
s.send(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))