html-python_socket通信

html-python_socket通信

python作为服务器:

import socket
import re


def seriveice_client(new_client_socket):
	"""为这个客户端返回数据"""
	#1.接受浏览器发送过来的请求,即HttP请求
	#GET / HTTP/1.1
	#....
	#解码http的数据
	request = new_client_socket.recv(1024).decode("utf-8")
	print(request)


	#处理数据 , 用切割函数,直接切割为列表,根据\n \r 切割
	request_lines = request.splitlines()
	#print("")
	#print
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
首先需要在nginx配置文件中添加一个新的location块,用于处理Python程序的请求。然后,需要安装uwsgi和uwsgi-plugin-python,以便能够在nginx和Python程序之间进行通信。具体的步骤如下: 1. 在nginx配置文件中添加以下代码: ``` location /mysql { uwsgi_pass unix:///tmp/uwsgi.sock; include uwsgi_params; uwsgi_param UWSGI_SCRIPT /path/to/python/program.py; } ``` 这个location块将处理所有以“/mysql”开头的请求,并将其传递给uwsgi服务器。 2. 安装uwsgi和uwsgi-plugin-python: ``` sudo apt-get install uwsgi uwsgi-plugin-python ``` 3. 创建一个Python程序,用于查询MySQL数据库,并将结果输出为HTML格式。例如: ``` #!/usr/bin/env python import MySQLdb # Connect to the database conn = MySQLdb.connect(host='localhost', user='username', passwd='password', db='database') # Execute a query cursor = conn.cursor() cursor.execute('SELECT * FROM table') # Print the results as an HTML table print '<table>' for row in cursor.fetchall(): print '<tr>' for col in row: print '<td>%s</td>' % col print '</tr>' print '</table>' # Close the database connection conn.close() ``` 注意:这个程序需要在服务器上运行,并且需要修改用户名、密码、数据库和表格名称,以匹配您的环境。 4. 启动uwsgi服务器: ``` uwsgi --socket /tmp/uwsgi.sock --plugin python --wsgi-file /path/to/python/program.py --callable app ``` 这个命令会启动一个uwsgi服务器,监听Unix socket“/tmp/uwsgi.sock”,并将请求传递给Python程序“/path/to/python/program.py”。 5. 重新加载nginx配置文件: ``` sudo service nginx reload ``` 现在,您应该能够通过访问“http://localhost/mysql”来查看Python程序的输出,它应该以HTML表格的形式显示MySQL数据库中的数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农-老七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值