Python(Socket)+ Unreal(HTTP)
- python(Socket):
- UE:Post请求并发送本机IP
上班咯,好久没记笔记了。。。
局域网 UE的apk,请求Python的Socket
跑起Socket ,UE发 POST请求
python(Socket):
import http.server
import socketserverclass MyRequestHandler(http.server.BaseHTTPRequestHandler):def do_POST(self):# 检查Content-Type是否为application/jsoncontent_type = self.headers.get('Content-Type')if content_type != 'application/json':self.send_response(400)self.end_headers()self.wfile.write(b'Bad Request: Content-Type should be application/json')return# 获取JSON数据print(self.headers)content_length = int(self.headers['Content-Length'])json_data = self.rfile.read(content_length)print(json_data)# 解析JSON数据try:data = json.loads(json_data)except json.JSONDecodeError:self.send_response(400)self.end_headers()self.wfile.write(b'Bad Request: Invalid JSON data')return# 在这里可以使用data进行其他处理print('Received JSON data:', data)# 返回成功的响应self.send_response(200)self.end_headers()self.wfile.write(b'JSON data received successfully')def start_http():server_address = ('0.0.0.0', 9912)# Specify the path to your SSL certificate and private key fileshttpd = socketserver.TCPServer(server_address, MyRequestHandler)# httpd.socket = ssl.wrap_socket(httpd.socket, server_side=True)# Start the serverprint(f"服务器运行于 {server_address[0]}:{server_address[1]}")httpd.serve_forever()if __name__ == "__main__":# start_bot()threading.Thread(target=start_http).start()pass
UE:Post请求并发送本机IP
标题获取IP地址的C++类
参考 获取本地IP方法
FString AMyActor::GetMyIpAddress()
{bool canBind = true; // 根据您的需求设置FString HostIp;ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);if (SocketSubsystem){TSharedRef<FInternetAddr> HostAddr = SocketSubsystem->GetLocalHostAddr(*GLog, canBind);HostIp = HostAddr->ToString(false); // 'false' 表示不包含端口号}if (!HostIp.IsEmpty()){UE_LOG(LogTemp, Log, TEXT("Local IP Address: %s"), *HostIp);return HostIp;}else{UE_LOG(LogTemp, Warning, TEXT("Unable to get local IP address."));return "";}
}
再配一个插件VaRest