socket ssh (不帶防止粘包的方法)
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author ian ying
# mail: [email protected]
import socket
import os
server = socket.socket()
server.bind(('localhost', 6969)) #繫結被監聽埠
server.listen(5) #監聽埠
while true:
prin程式設計客棧t("我要開始等**了")
conn, addr = server.accept() # 就是等待的意思
#conn就是客戶端連過來的時候,在伺服器端為其生成的乙個連線例項
print("**來了%s"% [conn, addr])
while true:
data = conn.recv(1024)
if not data:
print('client is lost.')
break
# res = os.popen(data).read() #popen就是開啟命令執行,read就是獲取結果
# with open('filename', 'r') as ret: #這兩行就 可以用過來傳輸檔案了。
# data = ret.read()
print('receive:',data)
conn.send(data.upper())
server.close()
socket client 模組
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author ian ying
# mail: [email protected]
import socket
client = socket.socket() #宣告socket型別,同時生成socket鏈結物件
client.connect(('localhost',6969)) #localhost就是本機位址
while true:
msg = input('input msg >>:').strip()
if len(msg) == 0: continue #檢查msg的資訊,防止無輸入資訊
#client.send(b"hello, world!") #傳送資訊
client.send(msg.encode('utf-8'))
data = client.recv(1024) #預設接受1024位元組,就是1k
# with open('filename', 'w') as ret: # 這兩行就 可以用過來傳輸檔案了。
# ret = data.write()
print(data.decode())
client.close() #關閉埠
防止粘包的socket_ssh.py
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author ian ying
# mail: [email protected]
impo soc程式設計客棧ket
import os
server = socket.socket()
server.bind(('localhost', 6969)) #繫結被監聽埠
server.listen(5) #監聽埠
while true:
print("我要開始等**了")
conn, addr = server.accept() # 就是等待的意思
#conn就是客戶端連過來的時候,在伺服器端為其生成的乙個連線例項
while true:
data = conn.recv(1024).decode()
print("**來了%s" % type(data))
# if type(data) is str:
# data = data.strip()
if not data:
print('client is lost.')
break
cmd_res = os.popen(data).read() #popen就是開啟命令執行,read就是獲取結果
cmd_res_size = str(len(cmd_res.encode("utf-8")))
print("before send",len(cmd_res),"size after encode", cmd_res_size)
if len(cmd_res) == 0:
print("there is no output.")
res_warning = "there is no output."
conn.send(res_warning.encode("utf-8"))
continue
else:
conn.send(cmd_res_size.encode("utf8"))
print(conn.recv(1024).decode()) #通過接收資料的形式來強制傳送緩衝區的資料,防止粘包。
# with open('filename', 'r') as ret: #這兩行就 可以用過來傳輸檔案了。
# data = ret.read()
#print('receive:',data)
print('receive:', data)
conn.send(cmd_res.encode("utf-8"))
# conn.send(bytes(cmd_res)) #不可行。傳輸的時候是需要encoding
server.close()
socket_client.py
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author ian ying
# mail: [email protected]
import socket
client = socket.socket() #宣告socket型別,同時生成socket鏈結物件
client.connect(('localhost',6969)) #localho程式設計客棧st就是本機位址
while true:
msg = input('input msg >>:').strip()
if len(msg) == 0: continue #檢查msg的資訊,防止無輸入資訊
#client.send(b"hello, world!") #傳送資訊
client.send(msg.encode('utf-8'))
received_size = client.recv(1024).decode() #用來記錄接受的資料大小
print("接收的資料大小", received_size)
received_cont = b''
received_cont_size = 0 # 用來判斷接受資料的大小
if received_size != "there is no output." :
client.send("準備好了,可以傳送。".encode()) #傳送確認資訊,以防止粘包
received_size = int(received_size) #資料需要變成int才能進行判斷
while received_size != received_cont_size: #判斷encode後的長度是否一致。
received_cont_for_test = client.recv(1024)
received_cont_size += int(len(received_cont_for_test))
received_cont = received_co + received_cont_for_test
print("當前結束後的資料大小為:", received_cont_size)
# print(received_cont_size)
else:
print("資料接收完成,接收的資料大小為:", received_cont_size)
print("接收的內容為:\n",received_cont.decode(),"\n")
else:
print("output:\n", received_size)
# data = client.recv(1024) #預設接受1024位元組,就是1k
# with open('filename', 'w') as ret: # 這兩行就 可以用過來傳輸檔案了。
# ret = data.write()
# print(data.decode())
# print(str(data))
client.close() #關閉埠
本文標題: python socket模組方法實現詳解
本文位址:
Python socket模組應用
socket 本文記錄了一些socket模組的簡單應用,對於具體原理還沒來得及深究。利用socket模組進行埠連線驗證和掃瞄 在linux中常用nc命令來進行遠端埠是否開放的驗證。但是這個命令並不是系統自帶的,所以還比較麻煩。如果利用python自帶的socket模組就可以比較自由地進行埠驗證了。是...
python socket 函式 模組
import socket socket 函式 1,語法格式 socket.socket family type proto family 套接字家族可以使af unix或者af inet type 套接字型別可以根據是tcp連線和udp連線分為sock stream或sock dgram prot...
python socket模組 監控埠
import socket,re 叫做非貪婪匹配,盡可能的少匹配 叫做貪婪匹配,盡可能的多匹配 a fenif1212nfi129f21f res re.compile d findall a print res hosts 1.1.1.1 90 2.2.2.2 8080 127.0.0.1 80 ...