socket是Python中的通信模块,用来计算机之间通信,目前在局域网中测试通过。
server端
# socket中的参数
# type 指建立连接的类型
# type = SOCK_STREAM:采用TCP
# type = SOCK_Dgram:采用UDP
# family 通信方式
# family = AF_INET 服务器之间的通信(这个参数指ipv4),还有个ipv6
# family = AF_INET6 这个指ipv6
# family = AF_UNIX unix不同进程间的通信
import socket
sk = socket.socket() # 创建socket对象
address = ('127.0.0.1',8080) # 写地址和端口
sk.bind(address) # 绑定地址和端口号
sk.listen(3) # 设置监听人数
conn,addr = sk.accept()
# print(conn)
# print(addr)
# accept阻塞,卡在这不走等待别人连接
# accept是一个元组,包含了socket对象和地址,所以用两个参数去接受
data = conn.recv(1024)
# recv在这里也会做一个阻塞,等待接收,没收到,就停在这里等
print(str(data,'utf8')) # 接收的时候,做编码转换
client端
import socket
sk = socket.socket() # 创建socket对象
address = ('127.0.0.1',8080) # 写上要查找的地址端口
sk.connect(address) # 进行连接
n = input('>>>')
sk.send(bytes(n,'utf8')) # 发送数据,必须为bytes类型,所以要做转换
sk.close() # 关闭这个客户端
server端
import socket
import subprocess
sk = socket.socket()
address = ('127.0.0.1',8080)
sk.bind(address) #
sk.listen(3)
while True:
conn, addr = sk.accept()
while True:
try:
data = conn.recv(1024)
except Exception:
break
if not data:
break
print(str(data, 'utf8'))
obj = subprocess.Popen(str(data,'utf8'),shell=True,stdout=subprocess.PIPE)
ruselt = obj.stdout.read()
ruselt_len = bytes(str(len(ruselt)),'utf8')
conn.sendall(ruselt_len)
conn.sendall(ruselt)
client端
import socket
sk = socket.socket()
address = ('127.0.0.1',8080)
sk.connect(address)
while True:
n = input('>>>')
if n == 'exit':
sk.close()
break
sk.send(bytes(n,'utf8'))
ruselt_len = int(str(sk.recv(1024),'utf8'))
data = bytes() # 这个相当于 data = 0
while len(data) != ruselt_len:
recv = sk.recv(1024)
data += recv
print(str(data,'gbk'))
server端
import socketserver
# socket server默认监听5个
class myserver(socketserver.BaseRequestHandler):
def handle(self): # 这个方法名不能变,固定的
while True:
conn = self.request
print(self.client_address)
while True:
data = conn.recv(1024)
print(str(data,'utf8'))
n = input('>>>')
# 下面要用 .encode 来确定编码格式
conn.sendall(bytes(n.encode('utf8')))
conn.close()
if __name__ == '__main__':
server = socketserver.ThreadingTCPServer(('127.0.0.1',8080),myserver)
server.serve_forever()
client端
import socket
sk = socket.socket()
address = ('127.0.0.1',8080)
sk.connect(address)
while True:
n = input('>>>')
if n == 'exit':
break
sk.sendall(bytes(n,'utf8'))
data = sk.recv(1204)
print(str(data,'utf8'))
sk.close()
server端
import socket
import os
sk = socket.socket()
address = ('127.0.0.1',8080)
sk.bind(address)
sk.listen(2)
DIR = os.path.dirname(os.path.abspath(__file__))
while True:
conn,addr = sk.accept()
while True:
data = conn.recv(1024)
cmd,filename,filesize = str(data,'utf8').split('|')
path = os.path.join(DIR,'example',filename)
filesize = int(filesize)
f = open(path,'ab')
send = 0
while send != filesize:
data = conn.recv(1024)
f.write(data)
send += len(data)
f.close()
client端
import socket
import os
sk = socket.socket()
address = ('127.0.0.1',8080)
sk.connect(address)
DIR = os.path.dirname(os.path.abspath(__file__))
while True:
n = input('>>>>').strip()
cmd,path = n.split('|')
path = os.path.join(DIR, path)
file_name = os.path.basename(path)
file_size = os.stat(path).st_size # 通过路径获取文件大小
file_info = 'post|%s|%s' % (file_name,file_size)
# print(file_info)
sk.sendall(bytes(file_info,'utf8'))
f = open(path,'rb')
send = 0
while send != file_size:
data = f.read(1024)
sk.sendall(data)
send += len(data)
f.close()
print('上传成功')
server端
import socket
sk = socket.socket()
address = ('172.16.0.135',8080)
sk.bind(address) #
sk.listen(3)
while True:
conn, addr = sk.accept()
print('lianjiechenggong ')
while True:
try:
data = conn.recv(1024)
except Exception:
break
if not data:
break
print(str(data, 'utf8'))
n = input('>>>')
conn.send(bytes(n,'utf8'))
client端
import socket
sk = socket.socket()
address = ('172.16.0.135',8080)
sk.connect(address)
while True:
n = input('>>>')
if n == 'exit':
sk.close()
break
sk.send(bytes(n,'utf8'))
data = sk.recv(1024)
print(str(data,'utf8'))
# 不能发送空,发过去server端还会处于阻塞状态