我需要在 python 上的 POST 网络服务器中修复什么?
我有一个脚本来获取 json 并在我的 ubuntu 服务器上使用白色 IP 运行它 我接受json,但不完全接受,接受后,脚本关闭连接并且不起作用 我认为问题是我错误地接收了数据包,但是由于我的经验很少,我不清楚为什么它不接受第二个数据包以及为什么它会在无限循环中关闭
import os
import socket
from pathlib import Path
from dotenv import load_dotenv
import json
#Init .env
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
#create webserver socket
def start_my_server():
socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_server.bind(('ip', port))
socket_server.listen(32768)
print('Working...')
global data
global HDRS
while True:
client_socket, address = socket_server.accept()
data = client_socket.recv(32768).decode('utf-8')
# content = 'Well done'.encode('utf-8')
HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
# content = load_page_from_get_request(data)
client_socket.send(HDRS.encode('utf-8'))
# a = client_socket.send(HDRS.encode('utf-8'))
# print(a, '+'*20)
client_socket.shutdown(socket.SHUT_WR)
load_page_from_get_request(32768)
# print('end')
# socket_server.close()
def load_page_from_get_request(request_data):
HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
HDRS_404 = 'HTTP/1.1 404 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
try:
with open('data.json', 'w') as output_file:
json.dump(data, output_file)
return HDRS.encode('utf-8')
except EOFError:
return HDRS_404.encode('utf-8')
# try
if __name__ == '__main__':
start_my_server()
I have a script to get json and run it on my ubuntu server with a white ip
I accept json, but not completely, and after this acceptance, the script closes the connection and does not work
I think the problem is that I receive packets incorrectly, but why it does not accept the second packet and why it closes in an infinite loop is not clear to me due to my little experience
import os
import socket
from pathlib import Path
from dotenv import load_dotenv
import json
#Init .env
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
#create webserver socket
def start_my_server():
socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_server.bind(('ip', port))
socket_server.listen(32768)
print('Working...')
global data
global HDRS
while True:
client_socket, address = socket_server.accept()
data = client_socket.recv(32768).decode('utf-8')
# content = 'Well done'.encode('utf-8')
HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
# content = load_page_from_get_request(data)
client_socket.send(HDRS.encode('utf-8'))
# a = client_socket.send(HDRS.encode('utf-8'))
# print(a, '+'*20)
client_socket.shutdown(socket.SHUT_WR)
load_page_from_get_request(32768)
# print('end')
# socket_server.close()
def load_page_from_get_request(request_data):
HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
HDRS_404 = 'HTTP/1.1 404 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'
try:
with open('data.json', 'w') as output_file:
json.dump(data, output_file)
return HDRS.encode('utf-8')
except EOFError:
return HDRS_404.encode('utf-8')
# try
if __name__ == '__main__':
start_my_server()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的脚本的最终版本
The end version of my script