python的socket模块_Python socket模块方法实现详解
這篇文章主要介紹了python socket模塊方法實現詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
socket ssh (不帶防止粘包的方法) #! /usr/bin/env python
# -*- coding:utf-8 -*-
# Author Ian Ying
# mail: kongqing.ying@yitu-inc.com
import socket
import os
server = socket.socket()
server.bind(('localhost', 6969)) #綁定被監聽端口
server.listen(5) #監聽端口
while True:
print("我要開始等電話了")
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: kongqing.ying@yitu-inc.com
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: kongqing.ying@yitu-inc.com
import socket
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: kongqing.ying@yitu-inc.com
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'))
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_cont + 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模块方法实现详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opening tables mysql
- 下一篇: python粘贴代码到word_Pyth