Python套接字服务器处理HTTPS请求

编程入门 行业动态 更新时间:2024-10-26 13:19:01
本文介绍了Python套接字服务器处理HTTPS请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Python 2.7和socket模块编写了一个套接字服务器.

I wrote a socket server using Python 2.7 and the socket module.

当我发出HTTP请求时,一切都按预期工作:服务器接受该请求并正确回答.但是,如果不是(例如)a,而是浏览a,我会收到某种加密的标头,并且我不知道如何告诉客户端服务器不支持HTTPS.

Everything works as expected when I issue an HTTP request: the server accepts it and answers correctly. But if instead of (let's say) a I browse for a I receive some kind of encrypted header and I don't know how to tell the client that HTTPS is not supported by the server.

我在Google上搜索了一下,但没有任何好处.我尝试用简单的HTTP进行回复,但浏览器显然忽略了该响应.

I googled a bit but nothing good. I tried to reply in plain HTTP but the response is clearly ignored by the browser.

如果有人能够向我展示一种告诉客户端没有SSL的方法,那将对我有真正的帮助.

If anyone would be able to show me a way to tell the client there's no SSL it would really help me.

谢谢.

推荐答案

我遇到了同样的问题.您必须这样在代码中配置"ssl"上下文:

I had the same problem. You have to configure "ssl" context within your code as such:

import socket, ssl HOST = "www.youtube" PORT = 443 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s_sock = context.wrap_socket(s, server_hostname=HOST) s_sock.connect((HOST, 443)) s_sock.send(" GET / HTTP/1.1\r\nHost: www.youtube\r\n\r\n ".encode()) while True: data = s_sock.recv(2048) if ( len(data) < 1 ) : break print(data) s_sock.close()

更多推荐

Python套接字服务器处理HTTPS请求

本文发布于:2023-08-07 03:48:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   Python   HTTPS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!