Django runserver 支持https

本篇博文主要介绍如何在Django的runserver服务器模式配置https访问

1.首先我们需要得到一张证书文件

参考:WINDOWS系统下创建自签名SSL证书 
并复制到项目的主目录下,本文中的证书文件的名称为server.crt,下文以此为例。

2.python环境准备

确认之前已经正确安装Python3,Django1.8及以上 
还需要安装的python库有:

django-extensions 
django-werkzeug-debugger-runserver 
pyOpenSSL 
执行pip install命令安装:

pip install django-extensions
pip install django-werkzeug-debugger-runserver
pip install pyOpenSSL
  • 1
  • 2
  • 3
3.配置settings.py

原来的settings.py 
 
加入以下两行:

    'werkzeug_debugger_runserver',
    'django_extensions',
  • 1
  • 2

修改后的settings.py 

4.开启cmd,工作目录切换到项目的主目录下

执行

python manage.py runserver_plus --cert server.crt 127.0.0.1:7000
  • 1

 
访问:https://127.0.0.1:7000/

可以看到我们已经可以已https的方式来访问了。 
如果使用Pycharm开发,可以做以下配置: 

socket 如何支持https

02-09

一个简单的http代理服务器 想在加上一个支持https的功能,请问怎么弄啊。rn网上查了一下用openssl但是基础太差 没学过 搞了好久都不行,rn请教大家帮忙指点一下 有没有别的简单的方式实现。或者谁能帮改程openssl的 学习一下。谢谢rn代码rn[code=c]// s.cpp : Defines the entry point for the console application.rn//rnrn#include "stdafx.h"rn#include "s.h"rnrn#ifdef _DEBUGrn#define new DEBUG_NEWrn#undef THIS_FILErnstatic char THIS_FILE[] = __FILE__;rn#endifrnrn/////////////////////////////////////////////////////////////////////////////rn// The one and only application objectrnrnCWinApp theApp;rnrnusing namespace std;rnrnrn#include rn#include rn#include rn#include rn//#include "SockInit.h"rnusing namespace std;rnrn#define MAX_REQUESTLINE_LEN 2048 // IE请求行长度rnrnvoid __cdecl ServerThreadProc(void *param);rnint TCPsend(SOCKET s,const char*buf,int len,int flags);rnint TCPrecv(SOCKET s,const char*buf,int len,int flags);rn/************************************************rnrn 调试环境:Visual C++6.0 SP6+platform SDK 2003 R2rnrn 程序名称:HTTP proxy Server(HttpProxySvr.cpp)rnrn 功能 :IE通过设置代理方式上网,IE所有请求均rn 先发到该代理服务器,代理服务器rn 将请求直接转发给webserver, rn 后从webserver读取响应后发回IErn rn**************************************************/rnrnint main()rnrn USHORT uPort=9999; // 通信端口rn rn WORD wVersionRequested;rn WSADATA wsaData;rn int err;rn rn wVersionRequested = MAKEWORD( 2, 2 );rn rn err = WSAStartup( wVersionRequested, &wsaData );rn if ( err != 0 ) rn rn return 0;rn rn rn rn rn if ( LOBYTE( wsaData.wVersion ) != 2 ||rn HIBYTE( wsaData.wVersion ) != 2 ) rn rn rn WSACleanup( );rn return 0; rn rn SOCKET sListen = socket(AF_INET,SOCK_STREAM,0);rn if(sListen == INVALID_SOCKET)rn rn printf("Failed socket(),error code :%d",WSAGetLastError());rn return 0;rn rnrn sockaddr_in sin;rn sin.sin_addr.S_un.S_addr = INADDR_ANY;rn sin.sin_family =AF_INET;rn sin.sin_port = htons(uPort);rn rn if(bind(sListen,(sockaddr*)&sin,sizeof(sin))==SOCKET_ERROR)rn rn printf("Failed bind(),error code :%d",WSAGetLastError());rn closesocket(sListen);rn return 0;rn rnrn int res =listen(sListen,200);rnrn sockaddr_in addrRemote;rn int len =sizeof(addrRemote);rn while(true)rn rn SOCKET sNew = accept(sListen,(sockaddr*)&addrRemote,&len);rn if(sNew == INVALID_SOCKET)rn rn printf("Failed accept(),error code :%d",WSAGetLastError());rn return 0;rn rn rn printf("收到一个IE连接(IP:%s)\n",inet_ntoa(addrRemote.sin_addr));rnrn // 创建一个线程处理该连接 (每客户单线程模式...)rn _beginthread(ServerThreadProc,0,(void*)sNew);rn rn rn closesocket(sListen);rnrn return 0;rnrnrnvoid __cdecl ServerThreadProc(void *param)rnrn SOCKET sock = (SOCKET)param; //与浏览器连接的套接口rnrn char request[MAX_REQUESTLINE_LEN];rn memset(request,0,MAX_REQUESTLINE_LEN);rn int res =recv(sock,request,MAX_REQUESTLINE_LEN,0); // 接收浏览器(客户端)发来的请求rn if(res==0)rn rn rn printf("连接断开\n");rn rn closesocket(sock);rn return;rn rn shutdown(sock,SD_RECEIVE);rn // 打印收到的请求rn printf("header length :%d header: %s\n",res,request);rn rn // 分析请求行(分离出WebServer域名)rn char szAddrName[1024]=0; //域名rn int i =11;rn while(request[i]!='/'&&ih_addr_list); rn rn nRet = connect(hsocket,(LPSOCKADDR)&saServer,sizeof(SOCKADDR_IN)); rn if(nRet==SOCKET_ERROR) rn rn printf("Failed connect(),error code:%d\n",WSAGetLastError());rn closesocket(hsocket); rn return; rn rn rn // 向webserver转发IE请求 rn nRet = TCPsend(hsocket,request,strlen(request),0);rn rn char dest[1000]; rn nRet=1; rn int senlen=0;rn while(nRet>0) rn rn memset(dest,0,1000);rn nRet=recv(hsocket,dest,sizeof(dest)-1,0); // 从webserver获取数据 rn if(nRet==0) rn rn printf("与webserver(%s)的连接关闭\n",szAddrName);rn rn break;rn rn else if(nRet==SOCKET_ERROR )rn rn printf("Failed recv(),error code:%d\n",WSAGetLastError()); rn break;rn rn dest[nRet]='\0';rnrn senlen = TCPsend(sock,dest,nRet,0); //将数据转发给IE浏览器rn if(senlen==0)rn rn sockaddr_in sin;rn int len = sizeof(sin);rn if(getpeername(sock,(sockaddr*)&sin,&len)== SOCKET_ERROR)rn rn printf("Failed getpeername(),error code:%d\n",WSAGetLastError());rn break;rn rn printf("与浏览器(%s)的一个连接关闭...\n",inet_ntoa(sin.sin_addr));rn break;rn rn else if(senlen==SOCKET_ERROR)rn rn printf("Failed send(),error code:%d\n",WSAGetLastError()); rn break;rn rnrn // printf("%s\n",dest);rnrn // end of while(nRet>0) rnrnrn closesocket(sock);rn closesocket(hsocket);rnrnrnrnint TCPsend(SOCKET s,const char*buf,int len,int flags)rnrn int n=0,sendCount=0;rn int length =len;rn if(buf==NULL)rn return 0;rn while(length>0)rn rn n=send(s,buf+sendCount,length,flags); //发送数据,rn if(n==SOCKET_ERROR)//网络出现异常rn rn printf("Failed send(),error code:%d\n",WSAGetLastError());rn break;rn rn rn length-=n;rn sendCount+=n; rn rnrn return sendCount; // 返回已发送的字节数rnrnrnint TCPrecv(SOCKET s,char *buf,int len,int flags)rnrn rn int nRev=0,recvCount=0;rn int length =len;rnrn if(buf==NULL)rn return 0;rnrn // 循环接收数据rn while(length>0)rn rn nRev =recv(s,buf+recvCount,length,flags);rn if(nRev==SOCKET_ERROR)//网络出现异常rn rn printf("Failed recv(),error code:%d\n",WSAGetLastError());rn break;rn rn length-=nRev;rn recvCount+=nRev;rn rnrn return recvCount; //返回接收到的字节数rnrnrn[/code]

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试