node1:/root/test#cat t2.py
# coding = utf-8
# -*- coding:utf-8 -*-
import socket
ip_port = ('192.168.137.2',8080)
BUFSIZE = 10
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#创建套接字
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(ip_port)#绑定地址
s.listen(5)#监听链接
print('server listening8080...')
while True: #无限等待连接
conn,addr = s.accept() #接受客户端连接
print(conn)
print(addr)
print('接到来自%s的链接'%addr[0])
while True: #通信循环,无限接受客户端信息
#print conn.getpeername()
try:
peeraddr=conn.getpeername()
msg = conn.recv(BUFSIZE) #接受消息的内容
except:
break;
if len(msg)==0:break #如果 不加,已连接的客户端突然断开,recv不再阻塞发生死循环
print '333333333333'
print msg
print '333333333333'
try:
conn.send('response:'+msg)
except:
print '555555555'
#conn.send(msg.upper())#服务端发送消息
conn.close()#关闭链接
s.close()#关闭套接字
import socket
import time
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("192.168.137.2",8080))
#print s.getsockopt()
print s.getsockname()
#s.sendall("this is shutdown test" + "\r\n")
s.send('8888777')
print dir(socket.herror)
#print dir(socket.herror)
# s.shutdown(socket.SHUT_RDWR)
#s.shutdown(socket.SHUT_RDWR)
print(socket.SHUT_RDWR)
print(socket.SHUT_RD)
print(socket.SHUT_WR)
print s.recv(90)
来源:CSDN
作者:zhaoyangjian724
链接:https://blog.csdn.net/zhaoyangjian724/article/details/104423153