前三個網上都有,最後一個本身作了一下,說實話本身思考仍是有不少收穫的。
先看最後一個:
多線程Web代理服務器
服務端:如圖,使用多線程,注意serverSocket.accept()不能寫在循環裏面,由於咱們使用tcp創建鏈接,創建一次鏈接就足夠,寫在循環裏面的話客戶端只能發送一次請求,而後就必須斷開再創建鏈接,html
from socket import * import threading """ ...忘記保存 可以接受http請求並返回信息,而且可以同時接受多個請求 """ serverPort = 12000 serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.bind(('localhost', serverPort)) serverSocket.listen(5) def connect(): print(threading.current_thread().getName(), " 等待接受數據中...") connectSocket, adress = serverSocket.accept() while True: message = connectSocket.recv(1024).decode() print(threading.current_thread().getName()," 接受到了", adress, "的數據", message) connectSocket.send(message.upper().encode()) # 多線程運行connect函數 threads = [] for i in range(5): threads.append(threading.Thread(target=connect)) threads[i].start()
客戶端:創建一次鏈接即可以一直髮送數據python
from socket import * ClientSocket = socket(AF_INET, SOCK_STREAM) ClientSocket.connect(('localhost', 12000)) # 模擬瀏覽器發送請求 while True: fileName = input("資源名:") ClientSocket.send(fileName.encode()) data = ClientSocket.recv(1024) print("接收到數據", data.decode()) with open("response2.txt", mode="a+", encoding="utf-8") as f: f.write(data.decode())
運行結果如圖,開啓多個客戶端,能夠持續發送信息:web
不過這裏有一個缺陷,就是一旦創建了鏈接,就只能與這個客戶端進行通訊,客戶端斷開以後服務端並不會斷開繼續等待下一個鏈接,因此咱們能夠採起下面的策略:瀏覽器
1.在客戶端加上若是請求的資源名爲xxx時,斷開鏈接服務器
2.在服務器檢測鏈接是否斷開,斷開了則恢復accept狀態多線程
更改代碼以下,便可解決上述問題:其實就是加了當輸入爲cut,斷開鏈接,關閉客戶端,關閉服務器端與客戶端的鏈接,這樣就能夠造成一個最大爲5的服務,而且客戶端能夠隨意進出。。。app
from socket import * import threading """ ...忘記保存 可以接受http請求並返回信息,而且可以同時接受多個請求 """ serverPort = 12000 serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.bind(('localhost', serverPort)) serverSocket.listen(5) def connect(): print(threading.current_thread().getName(), " 等待接受數據中...") connectSocket, adress = serverSocket.accept() while True: message = connectSocket.recv(1024).decode() print(threading.current_thread().getName(), " 接受到了", adress, "的數據", message) if message == "cut": # 斷開鏈接 ------------ connectSocket.close() break connectSocket.send(message.upper().encode()) # 多線程運行connect函數 threads = [] for i in range(5): threads.append(threading.Thread(target=connect)) threads[i].start()
客戶端socket
from socket import *
ClientSocket = socket(AF_INET, SOCK_STREAM)
ClientSocket.connect(('localhost', 12000)) # 模擬瀏覽器發送請求
while True:
fileName = input("資源名:")
ClientSocket.send(fileName.encode())
data = ClientSocket.recv(1024)
if fileName == "cut": # ---------斷開鏈接
ClientSocket.close()
break
print("接收到數據", data.decode())
with open("response2.txt", mode="a+", encoding="utf-8") as f:
f.write(data.decode())
Web服務器
暫時沒有構成http報文進行發送,而是簡單的運行兩個程序進行交互tcp
Server.py函數
"""" 任務說明:一個簡單的web服務器,有如下功能 1.使用TCP協議進行鏈接 2.接受http請求 3.對請求的文件進行響應 4.建立由請求的文件組成的Http響應報文 5.不存在文件則返回404 """ from socket import * # 建立tcp socket tcpSocket = socket(AF_INET, SOCK_STREAM) # 綁定地址 tcpSocket.bind(("127.0.0.1", 9999)) # 開始監聽 tcpSocket.listen(5) while True: print("等待鏈接中....") tcpConnect, adrr = tcpSocket.accept() print("接受到了來自:", adrr, "的數據") while True: try: data = tcpConnect.recv(1024) if not data: break print("接收到了數據", data) f = open(data, encoding="utf-8") result = f.read() # header = 'HTTP/1.1 200 OK\r\n\r\n' #tcpConnect.send(header.encode()) tcpConnect.send(result.encode()) except FileNotFoundError as e: tcpConnect.send("資源不存在".encode()) tcpConnect.close() print("鏈接結束")
Client.py
from socket import * ClientSocket = socket(AF_INET, SOCK_STREAM) ClientSocket.connect(('localhost', 9999)) # 模擬瀏覽器發送請求 while True: fileName = input("資源名:") ClientSocket.send(fileName.encode()) data = ClientSocket.recv(1024) print("接收到數據", data.decode()) with open("response2.txt", mode="a+", encoding="utf-8") as f: f.write(data.decode())
運行結果以下,能夠看到請求的文件存在時就返回文件內容,不存在就返回提示信息
Client:
Server
UDP,PING程序:
emmmm,這個不寫了,網上能夠搜索到,,
郵件客戶:
這個參考菜鳥教程的,
import smtplib from email.mime.text import MIMEText from email.header import Header mail_host = 'smtp.qq.com' # 中轉服務器 mail_user = '2429160671@qq.com' mail_pass = 'xzfwjkhtpsrydjdh' sender = '2429160671@qq.com' # 發送方 receivers = ['2429160671@qq.com'] # 接收方 message = MIMEText('python 測試', 'plain', 'utf-8') message['From'] = Header("ennio", 'utf-8') message['To'] = Header("測試", 'utf-8') subject = 'python smtp 測試' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 創建鏈接 smtpObj.login(mail_user, mail_pass) # 登錄受權 smtpObj.sendmail(sender, receivers, message.as_string()) # 發送郵件 print("發送成功") smtpObj.close() except smtplib.SMTPException: print("Error......") smtpObj.close()
原文出處:https://www.cnblogs.com/eenio/p/11494167.html