第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
6. 305 Use Proxy

被請求的資源必須通過指定的代理才能被訪問。Location 域中將給出指定的代理所在的 URI 信息,接收者需要重復發(fā)送一個單獨的請求,通過這個代理才能訪問相應資源。只有原始服務器才能建立305響應。HTTP/1.1 305 Use ProxyLocation: https://proxy.example.org:8080/

3. 小結(jié)

本章節(jié)主要介紹了 HTTPS 協(xié)議的核心流程以及數(shù)字證書的定義,HTTPS 協(xié)議相對于 HTTP 協(xié)議復雜很多,所以候選人需要抓住核心矛盾,針對非對稱加密和對稱加密的特點,闡述流程中每個步驟的關(guān)鍵作用,然后再根據(jù)個人理解補充細節(jié)。

3.6 pip3 show package-name

命令 pip3 show package-name 顯示名稱為 package-name 的第三方模塊的信息,示例如下:C:\> pip3 show requestsName: requestsVersion: 2.23.0Summary: Python HTTP for Humans.Home-page: https://requests.readthedocs.ioAuthor: Kenneth ReitzAuthor-email: me@kennethreitz.orgLicense: Apache 2.0Location: c:\python3\lib\site-packagesRequires: chardet, idna, certifi, urllib3在以上信息中,requires 列出 requests 模塊的依賴的模塊:chardet、idna、certifi、urllib3,當安裝 requests 模塊時,會自動安裝這 4 個模塊。

4.1 實例

<template> <view> <button @click="sendGet">發(fā)送請求</button> </view></template><script> export default { methods: { sendGet () { uni.request({ //示例地址,非真實地址 url: 'http://localhost:8080/api/getData', //必須為大寫,默認值為GET,GET請求可以不寫此參數(shù) method:'GET', data:{ openId:"4a96efrtgyt56Q89jiyth" }, success(res) { console.log(res) } }) } } }</script>

1.2 Java 代碼如何與 Tomcat 合作?

Tomcat 也可稱作 Servlet 容器,Servlet 是它與 Java 應用的橋梁,Tomcat 重點解決了 Http 的請求連接,使得 Java 應用可以更專注處理業(yè)務邏輯。Servlet 是一套規(guī)范,所以它主要的工作就是定義一些接口,每一個接口解決不同的邏輯功能。請求到達 Tomcat,Tomcat 的 Servlet 齒輪轉(zhuǎn)動(調(diào)用)起來,觸發(fā)了 Servlet 的應用代碼。

2. SAML 2.0 簡介

SAML2.0 基于 XML 標準,核心目標是在不同安全域(Security Domain)之間交換認證和授權(quán)數(shù)據(jù)。SAML2.0 的主要組成有:服務提供者(SP):類似于 OAuth2.0 中的資源服務,用來提供真正的商業(yè)服務;身份鑒別者(IDP):提供用戶的身份鑒別服務,確保用戶的身份真實可信;斷言消息(Assertions):用來描述認證的對象,比如用戶認證的時間、方式、基本及擴展信息(如:email、電話等);協(xié)議(Protocol):是一系列的 Request 和 Response 對象的合集,代表著某個行為需要執(zhí)行的操作序列;綁定(Binding):代表了 SAML 信息通過何種通訊協(xié)議被傳輸,比如 HTTP、HTTP-POST、SOAP等;元數(shù)據(jù)(MetaData):是 SAML 中的配置數(shù)據(jù),比如 IP 地址、綁定類型等。SAML 2.0 的認證過程如下:

3. 小結(jié)

在上面的請求內(nèi)容中,規(guī)定了第一個是 method 后面是 url 接著是 protocal/version ,這樣的約束就是 http 的協(xié)議,服務器收到請求就根據(jù)這個規(guī)則拆開解析。應用這樣的模式,web 的客戶端和服務端互相知道了請求的方法,地址,字符編碼,參數(shù),響應值等。

2.4 第三方插件

基于第三方插件,Nginx 可以完成各種各樣復雜的功能,全方位滿足程序員的想法。比如在 Nginx 中引入 lua 模塊,可以實現(xiàn)對 Http 請求更細粒度的限制,包括限速、限流、校驗認證等等。后續(xù),在 Nginx 上發(fā)展出來的 OpenResty 已經(jīng)應用到了微服務網(wǎng)關(guān)方向。

2.1 動態(tài) URL 傳參

在 url 的路徑 (path)部分可以作為動態(tài)參數(shù),傳遞給視圖函數(shù),如下面幾種寫法:# hello_app/urls.pyfrom django.urls import pathfrom . import viewsurlpatterns = [ path('articles/<int:year>/', views.year_archive), path('articles/<int:year>/<int:month>/', views.month_archive), path('articles/<int:year>/<int:month>/<slug:title>/', views.article_title),]注意上面的定義了匹配三個動態(tài) URL 的映射,每個動態(tài) URL 會匹配一個至多個參數(shù),每個動態(tài)值使用 <> 符號匹配,采用 <type:name> 這樣的形式。我們對應的視圖函數(shù)如下:from django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.def year_archive(request, year, *args, **kwargs): return HttpResponse('hello, {} archive\n'.format(year)) def month_archive(request, year, month, *args, **kwargs): return HttpResponse('hello, month archive, year={}, moth={}!\n'.format(year, month))def article_title(request, year, month, title, *args, **kwargs): return HttpResponse('hello, title archive, year={}, month={}, title={}!\n'.format(year, month, title))對于動態(tài)的 URL 表達式中,匹配到的值,比如上面的 year,month 和 title 可以作為函數(shù)的參數(shù)放到對應的視圖函數(shù)中,Django 會幫我們把匹配到的參數(shù)對應的放到函數(shù)的參數(shù)上。這里參數(shù)的位置可以任意寫,但是名字必須和 URL 表達式中的對應。[root@server first_django_app]# curl http://127.0.0.1:8881/hello/articles/1998/hello, 1998 archive[root@server first_django_app]# curl http://127.0.0.1:8881/hello/articles/1998/12/hello, month archive, year=1998, moth=12![root@server first_django_app]# curl http://127.0.0.1:8881/hello/articles/1998/12/test/hello, title archive, year=1998, month=12, title=test比如 URL 中有 3 個動態(tài)參數(shù),在視圖函數(shù)中只寫上兩個參數(shù)接收也是沒問題的,因為剩下的參數(shù)會被傳到 kwargs 中以 key-value 的形式保存:(django-manual) [root@server first_django_app]# cat hello_app/views.py...def article_title(request, year, month, *args, **kwargs): return HttpResponse('hello, title archive, year={}, month={}, kwargs={}\n'.format(year, month, kwargs))# 啟動服務,再次請求后[root@server first_django_app]# curl http://127.0.0.1:8881/hello/articles/1998/12/test/hello, title archive, year=1998, month=12, kwargs={'title': 'test'}上述介紹的動態(tài) URL 匹配格式 <type:name> 中,Django 會對捕捉到的 URL 參數(shù)進行強制類型裝換,然后賦給 name 變量,再傳到視圖函數(shù)中。其中 Django 框架中支持的轉(zhuǎn)換類型有:str:匹配任意非空字符,不能匹配分隔符 “/”;int:匹配任意大于0的整數(shù);slug:匹配任意 slug 字符串, slug 字符串可以包含任意的 ASCII 字符、數(shù)字、連字符和下劃線等;uuid:匹配 UUID 字符串;path:匹配任意非空字符串,包括 URL 的分隔符 “/”。

2.1 安裝 uwsgi

python web 服務必須通過 uwsgi 協(xié)議才能進行訪問,因此需要安裝 uwsgi 服務來轉(zhuǎn)發(fā) python 的 http 請求。因此,第一步我們要安裝 uwsgi 服務:# 激活虛擬環(huán)境$ pyenv activate env-3.8.1# 安裝uwsgi服務$ pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple

SOCKET 協(xié)議

Socket 是傳輸層協(xié)議的具體軟件實現(xiàn),它封裝了協(xié)議底層的復雜實現(xiàn)方法,為開發(fā)人員提供了便利的網(wǎng)絡(luò)連接。Socket 是網(wǎng)絡(luò)編程的基石,像 Http 的請求,MySQL 數(shù)據(jù)庫的連接等絕大部分的網(wǎng)絡(luò)連接都是基于 Socket 實現(xiàn)的。

3.2 LimitOffsetPagination

前端訪問網(wǎng)址形式:GET http://127.0.0.1/api/students/?limit=100&offset=400可以在子類中定義的屬性:default_limit: 默認限制,默認值與PAGE_SIZE設(shè)置為一致;limit_query_param limit:參數(shù)名,默認 'limit;offset_query_param: offset 參數(shù)名,默認 ‘offset’;max_limit :最大 limit 限制,默認 None。from rest_framework.pagination import LimitOffsetPaginationclass StudentViewSet(ModelViewSet): queryset = StudentsModel.objects.all() serializer_class = StudentsSerializer pagination_class = LimitOffsetPagination

3.2 Cache Control

Spring Security 默認包含「Cache Control」頭。如果只需要緩存指定類型的內(nèi)容,我們可以通過 HttpServletResponse.setHeader(String,String) 方式指定緩存項,如 CSS、JavaScript、圖片等。我們也可以直接禁用掉「Cache Control」頭,方式如下:@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) { http.headers(headers -> headers.cacheControl(cache -> cache.disable())); }}

2. web 交互的前期

在 Ajax 出現(xiàn)之前,我們也有網(wǎng)頁,我們的前后端信息也需要交互,那么我們是怎么做的呢?當時的做法是:刷新(重載)或跳轉(zhuǎn)頁面。舉兩個例子:當我們填寫完 form 表單的時候,submit 提交表單,這個時候瀏覽器會進行頁面刷新或跳轉(zhuǎn),反饋給用戶表單提交是否成功。當我們在頁面上點擊跳轉(zhuǎn)慕課網(wǎng)鏈接(一般是 a 標簽,或者導航欄輸入),那么頁面會刷新或者跳轉(zhuǎn)到慕課網(wǎng)上去。也許這個時候你會覺得奇怪,交互一次就需要刷新一次?這未免也太過笨拙!是的,如你所見,在 Ajax 之前,HTTP 請求對應著頁面,一次 HTTP 請求也就意味著需要請求一個頁面。當然,人往高處走,總會有更加先進的辦法來解決當前的問題。因此,逐漸的,我們有了 Ajax。

2.2 代碼實現(xiàn)

1. 創(chuàng)建 maven 工程:pom 文件的 jar 包坐標如下:<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency></dependencies>2. 連接數(shù)據(jù)庫的工具類:@Componentpublic class ConnectionUtils { private ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); @Autowired private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } /** * 獲取當前線程上的連接 * @return */ public Connection getThreadConnection() { try{ //1.先從ThreadLocal上獲取 Connection conn = tl.get(); //2.判斷當前線程上是否有連接 if (conn == null) { //3.從數(shù)據(jù)源中獲取一個連接,并且存入ThreadLocal中 conn = dataSource.getConnection(); conn.setAutoCommit(false); tl.set(conn); } //4.返回當前線程上的連接 return conn; }catch (Exception e){ throw new RuntimeException(e); } } /** * 把連接和線程解綁 */ public void removeConnection(){ tl.remove(); }}3. 實體類 Account:public class Account implements Serializable { //數(shù)據(jù)id private Integer id; //賬號編碼 private String accountNum; //賬號金額 private Float money; //省略get 和set 方法}4. 持久層 dao 和 dao 的 實現(xiàn)類://dao的接口public interface IAccountDao { /** * 更新 * @param account */ void updateAccount(Account account); /** * 根據(jù)編號查詢賬戶 */ Account findAccountByNum(String accountNum);}//dao的實現(xiàn)類@Repositorypublic class AccountDaoImpl implements IAccountDao { //dbutil的查詢工具類 @Autowired private QueryRunner runner; //連接的工具類 @Autowired private ConnectionUtils connectionUtils; public void setRunner(QueryRunner runner) { this.runner = runner; } public void setConnectionUtils(ConnectionUtils connectionUtils) { this.connectionUtils = connectionUtils; } //修改賬號 public void updateAccount(Account account) { try{ runner.update(connectionUtils.getThreadConnection(),"update account set accountNum=?,money=? where id=?",account.getAccountNum(),account.getMoney(),account.getId()); }catch (Exception e) { throw new RuntimeException(e); } } //根據(jù)賬號查詢 public Account findAccountByNum(String accountNum) { try{ List<Account> accounts = runner.query(connectionUtils.getThreadConnection(),"select * from account where accountNum = ? ",new BeanListHandler<Account>(Account.class),accountNum); if(accounts == null || accounts.size() == 0){ return null; } if(accounts.size() > 1){ throw new RuntimeException("結(jié)果集不唯一,數(shù)據(jù)有問題"); } return accounts.get(0); }catch (Exception e) { throw new RuntimeException(e); } }}代碼解釋: AccountDaoImpl 類上面的注解 @Repository 表示使用注解實例化此類,并交給 Spring 的容器管理。5. 業(yè)務類 Service 和 Service 的實現(xiàn)類://業(yè)務接口public interface IAccountService { /** * 轉(zhuǎn)賬 * @param sourceAccount 轉(zhuǎn)出賬戶名稱 * @param targetAccount 轉(zhuǎn)入賬戶名稱 * @param money 轉(zhuǎn)賬金額 */ void transfer(String sourceAccount, String targetAccount, Integer money);}//業(yè)務實現(xiàn)類@Servicepublic class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; public void setAccountDao(IAccountDao accountDao) { this.accountDao = accountDao; } public void transfer(String sourceAccount, String targetAccount, Integer money) { Account source = accountDao.findAccountByNum(sourceAccount); Account target = accountDao.findAccountByNum(targetAccount); source.setMoney(source.getMoney()-money); target.setMoney(target.getMoney()+money); accountDao.updateAccount(source); accountDao.updateAccount(target); System.out.println("轉(zhuǎn)賬完畢"); }}代碼解釋:AccountServiceImpl 類上面的注解 @Service 表示使用注解實例化此類,并交給 Spring 的容器管理。6. 事務管理器類@Component@Aspectpublic class TransactionManager { @Autowired private ConnectionUtils connectionUtils; public void setConnectionUtils(ConnectionUtils connectionUtils) { this.connectionUtils = connectionUtils; } @Pointcut("execution(* com.offcn.service.impl.*.*(..))") private void pt1() {} /** * 開啟事務 */ @Before("pt1()") public void beginTransaction(){ try { System.out.println("開啟事務"); connectionUtils.getThreadConnection().setAutoCommit(false); }catch (Exception e){ e.printStackTrace(); } } /** * 提交事務 */ @AfterReturning("pt1()") public void commit(){ try { System.out.println("提交事務"); connectionUtils.getThreadConnection().commit(); }catch (Exception e){ e.printStackTrace(); } } /** * 回滾事務 */ @AfterThrowing("pt1()") public void rollback(){ try { System.out.println("回滾事務"); connectionUtils.getThreadConnection().rollback(); }catch (Exception e){ e.printStackTrace(); } } /** * 釋放連接 */ @After("pt1()") public void release(){ try { System.out.println("釋放連接"); connectionUtils.getThreadConnection().close();//還回連接池中 connectionUtils.removeConnection(); }catch (Exception e){ e.printStackTrace(); } }}代碼解釋:此類通過注解 @Componet 實例化,并且交由 Spring 容器管理,@Aspect 表明它是一個切面類。而下面的注解 @Pointcut 和其余的方法上的各個通知注解,在上面也已經(jīng)介紹過,這里不做贅述了。主要專注點在于每個注解的通知方法內(nèi)部引入切入點的表達式方式。7. 配置文件:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置QueryRunner--> <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"></bean> <!-- 配置數(shù)據(jù)源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--連接數(shù)據(jù)庫的必備信息--> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/transmoney"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 注解掃描工程下的包路徑--> <context:component-scan base-package="com.offcn"></context:component-scan> <!-- 注解代理模式 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>配置文件說明:dataSource: 采用 c3p0 數(shù)據(jù)源,大家一定要注意數(shù)據(jù)庫的名稱與賬號名和密碼;queryRunner: dbutils 第三方框架提供用于執(zhí)行 sql 語句,操作數(shù)據(jù)庫的一個工具類;context:component-scan: 此注解表示注解方式初始化容器掃描的包路徑;aop:aspectj-autoproxy: 此注解表示開啟代理模式8. 測試類代碼@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml")public class AccountServiceTest { @Autowired private IAccountService accountService; @Test public void testTransfer(){ accountService.transfer("622200009999","622200001111",100); }}測試結(jié)果:執(zhí)行代碼后結(jié)果:可以看到,我們通過注解方式配置 Spring 的 AOP 相關(guān)配置,同樣實現(xiàn)了對于數(shù)據(jù)的操作。

4. 小結(jié)

今天我們介紹了 Web 編程的相關(guān)術(shù)語,重點介紹了 HTTP 協(xié)議和 URL。接下來,我們介紹了網(wǎng)站的一個運行原理和開發(fā)的基本流程,有了這些知識儲備后,我們就可以正式開始學習 Django 了。

2.1 居中布局

當你希望 View 能夠在父布局中居中擺放時,你可以有以下3種屬性選擇:android:layout_centerHorizontal=“true”:這個屬性會讓你的View在父布局中水平居中,如上圖中紅色 View 所示,由于 RelativeLayout 占滿全屏,所以它最終會在屏幕的水平方向上居中顯示。android:layout_centerVertical=“true”:這個屬性會讓你的View在父布局中垂直居中,如上圖中黃色 View 所示,它最終會在屏幕的垂直方向上居中顯示。android:layout_centerInParent="true"看到這里,聰明的你應該能猜到,接下來就是在兩個方向上居中顯示。沒錯,這個屬性能夠讓你在父布局中整體居中顯示,如上圖的藍色 View 所示,它最終將擺放在屏幕的正中央。參考代碼如下:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:background="#F75549" android:text="centerHorizontal" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:background="#F1E14D" android:text="centerVertical" android:textSize="20sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#14CEE6" android:text="centerInParent" android:textSize="20sp" /></RelativeLayout>

4. 啟動 Nginx 服務

安裝完成后,Nginx 的文件默認全在 --prefix 指定的目錄中,即 /root/nginx。編譯出的 Nginx 二進制文件在 /root/nginx/sbin 目錄下,默認的配置文件為 /root/nginx/conf/nginx.conf。我們可以直接啟動 Nginx:$ cd /root/nginx/sbin$ ./nginx$ curl http://localhost # 測試 Nginx 服務是否啟動成功如果最后一步測試,發(fā)現(xiàn)返回 403(權(quán)限拒絕)的結(jié)果,我們可以修改下 nginx.conf 的配置,將 nginx.conf 中第一行 user 指令參數(shù)設(shè)置為 root,然后在重啟或者熱加載 Nginx 并執(zhí)行 curl 請求,查看結(jié)果。$ cd /root/nginx/conf$ vim nginx.conf$ cat nginx,conf# 指定nginx以root用戶啟動user root;...# 這次就能正確返回'Welcome to nginx'這樣的信息了$ curl http://localhost另外從瀏覽器上直接請求,也可以看到歡迎頁面。Tips:對于百度、阿里、騰訊這樣的云主機,需要事先放通 80 端口,允許外面通過 80 端口訪問服務,以及數(shù)據(jù)從 80 端口出去。

1. 初始配置

在前面搭建好 Nginx 環(huán)境后,編譯的 Nginx 根路徑為 /root/nginx,那么對應的配置文件為 /root/nginx/conf/nginx.conf ,直接用 cat 命令查看這里的配置文件內(nèi)容(刪除掉了原配置文件中的英文注釋,并對主要配置項增加中文注釋): $ cat /root/nginx/conf/nginx.conf # 啟動的worker進程數(shù) worker_processes 1; # 設(shè)置每個worker進程的最大連接數(shù),它決定了Nginx的并發(fā)能力 events { worker_connections 1024; } # http塊配置 http { include mime.types; default_type application/octet-stream; # 注釋了日志格式的配置,使用默認 ... sendfile on; # 重要參數(shù),是一個請求完成之后還要保持連接多久,不是請求時間多久, # 目的是保持長連接,減少創(chuàng)建連接過程給系統(tǒng)帶來的性能損耗 keepalive_timeout 65; # server塊配置 server { # 監(jiān)聽80端口 listen 80; server_name localhost; # 匹配url /,會在html目錄下,訪問index.html或index.htm文件 location / { root html; index index.html index.htm; } # 指定500 502 503 504出錯的錯誤頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

3.2 相機布局

目前市面上有很多相機 App,各種布局千變?nèi)f化,大家完全可以按照自己的喜好來進行設(shè)計。這里只做一個拍照預覽和拍照按鈕。<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <SurfaceView android:id="@+id/sfv_preview" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/btn_take" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:text="拍照" /></FrameLayout>

2.2 使用 uwsgi 工具部署和安裝 Django 服務

安裝 uwsgi : pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple。使用 uwsgi 啟動 Django 服務,同樣有直接使用命令行啟動和使用配置文件啟動兩種方式:# 新建uwsgi的pid保存目錄(django-manual) [root@server first_django_app]# mkdir uwsgi/(django-manual) [root@server first_django_app]# cat uwsgi.ini[uwsgi]# socket = 0.0.0.0:8888# 使用http協(xié)議訪問http = 0.0.0.0:8888# 指定執(zhí)行的目錄chdir = /root/django-manual/first_django_app# 非常重要,指定執(zhí)行的wsgi.py文件module = first_django_app.wsgi master = trueprocesses = 5threads = 5vacuum = truestats=%(chdir)/uwsgi/uwsgi.statuspidfile=%(chdir)/uwsgi/uwsgi.pid# 啟動 django 服務 (django-manual) [root@server first_django_app]# uwsgi -d --ini uwsgi.ini[uWSGI] getting INI configuration from uwsgi.ini(django-manual) [root@server first_django_app]# ps -ef | grep uwsgiroot 10250 1 4 19:32 ? 00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.iniroot 10282 10250 0 19:32 ? 00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.iniroot 10283 10250 0 19:32 ? 00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.iniroot 10284 10250 0 19:32 ? 00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.iniroot 10285 10250 0 19:32 ? 00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.iniroot 10286 10250 0 19:32 ? 00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.iniroot 10287 10250 0 19:32 ? 00:00:00 /root/.pyenv/versions/django-manual/bin/uwsgi -d --ini uwsgi.iniroot 10344 4650 0 19:33 pts/3 00:00:00 grep --color=auto uwsgi# 停止進程用--stop,重載用--reload(django-manual) [root@server first_django_app]# uwsgi --stop uwsgi/uwsgi.pid (django-manual) [root@server first_django_app]# ps -ef | grep uwsgiroot 10566 4650 0 19:35 pts/3 00:00:00 grep --color=auto uwsgiTips:如果在配置文件中使用 socket 監(jiān)聽端口,則需要使用 nginx 轉(zhuǎn)發(fā) http 協(xié)議為 uwsgi 協(xié)議 才行在瀏覽器中訪問。

3. 路徑(Endpoint)

路徑即"終點"(endpoint),是訪問 API 的具體網(wǎng)址,通過訪問每個網(wǎng)址,可以獲取到相應的資源(resource)。在師生管理系統(tǒng)中,所謂資源,就是我們想獲取的信息,比如獲取 3 年 2 班所有學生姓名,獲取小明的年齡、成績等。路徑須滿足以下規(guī)范:1. 資源路徑中應當使用名詞,杜絕動詞。資源路徑中的名詞,應當與數(shù)據(jù)庫的表名相對應。以下路徑中包含動詞,是不符合規(guī)范的例子,在實際工作中,應當避免。/getStudents :獲取學生信息/listTeachers :獲取老師信息/retreiveStudentByID?Id=2020 :獲取ID為2020的學生信息對于資源的操作,應該通過 HTTP 中的不同方法來區(qū)分處理資源的動作,資源路徑中應當只包含名詞。GET /students :將返回所有學生信息POST /students :將新增的學生信息存入數(shù)據(jù)庫GET /students/4 :獲取編號為4號的學生信息PATCH(或)PUT /students/4 :更新編號為4的學生信息2. API 中的名詞應該使用復數(shù)。無論是子資源或者是所有資源。例如:獲取單個學生信息:http://www.demo.com/students/1 :獲取編號為1的學生信息獲取所有學生信息: http://www.demo.com/students :獲取所有學生信息

2. 網(wǎng)站運行原理

在了解上面這些基本術(shù)語后,我們介紹下當在瀏覽器中敲下 www.baidu.com 這個 URL,到百度返回給我們搜索首頁,這個過程中究竟發(fā)生了哪些事情?解析輸入 URL 中包含的信息,比如 HTTP 協(xié)議和域名 (baidu.com);客戶端先檢查本地是否有對應的 IP 地址,若找到則返回響應的 IP 地址。若沒找到則請求在 ISP 的 DNS 服務器上。如果還沒找到,則請求會被發(fā)向根域名服務器,直到找到對應的 IP 地址;瀏覽器在 DNS 服務器中找到對應域名的 IP 地址,然后結(jié)合 URL 中的端口(沒有指明端口則使用默認端口,HTTP 協(xié)議的默認端口是 80,HTTPS 的默認端口是 443) 組成新的請求 URL,并與百度的 Web 服務建立 TCP 連接;瀏覽器根據(jù)用戶操作向百度的 Web 服務器發(fā)送 HTTP 請求;Web 服務器接收到該請求后會根據(jù)請求的路徑查找對應的 Web 資源并返回;最后客戶端瀏覽器將這些返回的 Web 信息 (包括圖片、HTML 靜態(tài)頁面,JS 等)組織成用戶可以查看的網(wǎng)頁形式,最后就得到了我們熟悉的那個 百度一下,你就知道 的搜索主頁了。

1.前言

ThinkPHP 支持 Console 應用,通過命令行的方式執(zhí)行一些 URL 訪問不方便或者安全性較高的操作。前面學習的接口封裝,都是基于 HTTP 請求的,請求時間是會有超時時間的,若使用命令行可以在后臺進程運行,而不是依賴于訪問進程,ThinkPHP 命令行提供了一些方便的工具 ,下面介紹如何使用 ThinkPHP 命令行。

1. 為什么需要Cookie和Session?

在 Web 程序中,對會話的跟蹤是很一件非常重要的事情。通常,一個用戶的所有請求操作都應該屬于同一個會話,而另一個用戶的所有請求操作則應該屬于另一個會話,二者不能互不干擾。然而大部分的 Web 應用程序都是使用HTTP 協(xié)議傳輸數(shù)據(jù)的。HTTP協(xié)議是無狀態(tài)的協(xié)議。一旦數(shù)據(jù)交換完畢,客戶端與服務器端的連接就會關(guān)閉,再次交換數(shù)據(jù)需要建立新的連接。這就意味著服務器無法從連接上跟蹤會話。如果 Web 服務上沒有這種的會話追蹤功能,那么大部分網(wǎng)站都會陷入一片混亂。特別是對于電商網(wǎng)站而言,如果我添加一次購物車就需要登錄一次確認身份,那對于用戶體驗而言是糟糕透的。會話(Session)跟蹤是 Web 程序中常用的技術(shù),用來跟蹤用戶的整個會話。常用的會話跟蹤技術(shù)是Cookie與Session。Cookie通過在客戶端記錄信息確定用戶身份,Session通過在服務器端記錄信息確定用戶身份。正是有了 Cookie 和 Session 這樣的會話跟蹤技術(shù),彌補了 HTTP 協(xié)議無狀態(tài)的不足,才使得我們可以無差別的訪問網(wǎng)站,即只需要一次登錄,后續(xù)所有的操作通過 Cookie 或者 Session 便能自動識別為該用戶,并維持在一個會話中。

2.4 概括

綜合上面的解釋,總結(jié)一下什么是 REST:服務端使用 URI 定位資源;客戶端和服務器之間,傳遞這種資源的某種表現(xiàn)形式;客戶端通過四個 HTTP 方法 (GET/POST/PUT/DELETE),對服務器端資源進行操作,實現(xiàn) “資源表述狀態(tài)轉(zhuǎn)移”。

head 頭部

本章節(jié)講解 HTML 中的 head 標簽,以及 head 內(nèi)包含關(guān)于 HTTP 協(xié)議的標簽-meta。<head> 定義文檔的頭部,它是所有頭部元素的容器。<head> 中的元素可以引用腳本、指示瀏覽器在哪里找到樣式表、提供元信息等等。

4. 小結(jié)

本節(jié)主要內(nèi)容如下:X.509 是一種證書格式規(guī)范;HTTPS 是 X.509 在 HTTP 安全上的應用;Spring Security 支持開啟 Web 客戶端的證書認證集成功能。至此,關(guān)于 Spring Security 的認證部分就結(jié)束了,下節(jié)開始,我們討論 Spring Security 的第二大功能「鑒權(quán)」。

3. 小結(jié)

本節(jié)我們介紹了 python 的虛擬環(huán)境搭建,使用 django 框架創(chuàng)建第一個 web 工程,然后使用 uwsgi 服務運行該 django 項目,最后我們使用 Nginx 服務將 http 請求轉(zhuǎn)發(fā)到 uwsgi 容器中的 django 工程去執(zhí)行。最后我們從瀏覽器中成功訪問了了 django 工程的首頁,這表明著我們部署 django 項目成功。

5.4 創(chuàng)建商品頁面

我們 resource/templates 目錄下新建商品頁面 goods.ftl ,先不必實現(xiàn)具體功能,代碼如下:實例:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>商品列表</title></head><body>商品列表</body></html>此時我們啟動項目,然后訪問 http://127.0.0.1:8080/goods ,即可顯示對應頁面內(nèi)容。

直播
查看課程詳情
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號