1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import
socket #导入socket类
HOST =
''
#定义侦听本地地址口(多个IP地址情况下),这里表示侦听所有,也可以写成
0.0
.
0.0
PORT =
50007
#Server端开放的服务端口
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #选择Socket类型和Socket数据包类型
s.bind((HOST, PORT)) #绑定IP地址和端口
s.listen(
1
) #定义侦听数开始侦听(实际上并没有效果)
conn, addr = s.accept() #定义实例,accept()函数的返回值可以看上面的socket函数说明
print
'Connected by'
, addr
while
1
:
data = conn.recv(
1024
) #接受套接字的数据
if
not data:
break
#如果没有数据接收,则断开连接
print
'revc:'
,data #发送接收到的数据
conn.sendall(data) #发送接收到的数据
conn.close() #关闭套接字
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
socket
HOST =
'192.168.1.13'
PORT =
50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while
True:
user_input = raw_input(
'msg to send:'
).strip() #由User输入要发送的数据
s.sendall(user_input)
data = s.recv(
1024
)
print
'Received'
, repr(data)
s.close()
|
1
2
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py
===>光标在此处处于等待状态
|
1
2
3
4
5
6
7
8
9
10
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py
msg to send:The first msg. ===>User输入数据
Received
'The first msg.'
===>Server端返回的数据
msg to send:The second msg.
Received
'The second msg.'
msg to send:The third msg.
Received
'The third msg.'
msg to send:I'm A.
Received
"I'm A."
msg to send: ===>继续等待User输入数据
|
1
2
3
4
5
6
7
8
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5/[
2
]sec_4_ver2(单线程,交互式,阻塞模
一般演示)$ python server4.py
Connected by (
'192.168.1.13'
,
52645
)
revc: The first msg. ===>接收到用户发送的数据
revc: The second msg.
revc: The third msg.
revc: I'm A.
===>光标在此处处于等待状态
|
1
2
3
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py
msg to send:I'm B
===>光标在此处处于等待状态
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import
socket
HOST =
''
PORT =
50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(
1
)
while
1
:
conn, addr = s.accept() #在循环中接受Client端连接的请求
print
'Connected by'
, addr
while
True: #再做一个内部的循环
data = conn.recv(
1024
)
print
'Received'
,data
if
not data:
break
conn.sendall(data)
conn.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
socket
HOST =
'192.168.1.13'
PORT =
50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while
True:
user_input = raw_input(
'msg to send:'
).strip()
s.sendall(user_input)
data = s.recv(
1024
)
print
'Received'
, repr(data)
s.close()
|
1
2
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py
===>光标在此处处于等待状态
|
1
2
3
4
5
6
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py
msg to send:Hello!
Received
'Hello!'
msg to send:I'm Client A.
Received
"I'm Client A."
msg to send: ===>继续等待User输入数据
|
1
2
3
4
5
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py
Connected by (
'192.168.1.13'
,
52647
)
Received Hello!
Received I'm Client A.
===>光标在此处处于等待状态
|
1
2
3
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py
msg to send:I'm Client B.
===>光标在此处处于等待状态
|
1
2
3
4
5
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py
Connected by (
'192.168.1.13'
,
52647
)
Received Hello!
Received I'm Client A.
===>光标在此处处于等待状态
|
1
2
3
4
5
6
7
8
9
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py
msg to send:Hello!
Received
'Hello!'
msg to send:I'm Client A.
Received
"I'm Client A."
msg to send:^CTraceback (most recent call last):
File
"client4.py"
, line
10
,
in
<module>
user_input = raw_input(
'msg to send:'
).strip()
KeyboardInterrupt
|
1
2
3
4
5
6
7
8
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python server4.py
Connected by (
'192.168.1.13'
,
52647
)
Received Hello!
Received I'm Client A.
Received
Connected by (
'192.168.1.13'
,
52648
)
Received I'm Client B. ===>成功接收到来自Client B端发送的数据
===>光标在此处处于等待状态
|
1
2
3
4
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day5$ python client4.py
msg to send:I'm Client B.
Received
"I'm Client B."
msg to send: ===>光标在此处处于等待状态
|