파이썬 스크립트로 작동하는 API의 샘플 자바 코드를 조정하려고합니다. Java 코드가 작동하고 파이썬에서 소켓 연결을 수행 할 수 있지만 xml 요청을 성공적으로 보낼 수 있도록 파이썬에서 문자열을 변환하는 방법을 알아낼 수는 없습니다. 구조체를 사용해야하지만 지난 주에 아직 이해할 수 없었다고 확신합니다.은 파이썬 클라이언트를 통해 java 서버와 통신합니다.
또한 처음에는 요청의 길이를 보내고 요청을 보내야하지만 다시 한번 서버 프로그램에 대한 성공적인 요청을 표시 할 수는 없었습니다.
public void connect(String host, int port) {
try {
setServerSocket(new Socket(host, port));
setOutputStream(new DataOutputStream(getServerSocket().getOutputStream()));
setInputStream(new DataInputStream(getServerSocket().getInputStream()));
System.out.println("Connection established.");
} catch (IOException e) {
System.out.println("Unable to connect to the server.");
System.exit(1);
}
}
public void disconnect() {
try {
getOutputStream().close();
getInputStream().close();
getServerSocket().close();
} catch (IOException e) {
// do nothing, the program is closing
}
}
/**
* Sends the xml request to the server to be processed.
* @param xml the request to send to the server
* @return the response from the server
*/
public String sendRequest(String xml) {
byte[] bytes = xml.getBytes();
int size = bytes.length;
try {
getOutputStream().writeInt(size);
getOutputStream().write(bytes);
getOutputStream().flush();
System.out.println("Request sent.");
return listenMode();
} catch (IOException e) {
System.out.println("The connection to the server was lost.");
return null;
}
}
감사합니다! 실제로 챔피언처럼 일한 것은 실제로 서버에서 뭔가를 얻었지만 실제로는 오류 메시지가 아니라 내가 얻은 것보다 낫다고 생각합니다. 또한 send_string의 빠른 교정은 [send :] 대신 [sent :]가되어야합니다. – enderv
@enderv 예, 수정 해 주셔서 감사합니다. 다행 했어. 좀 더 구체적인 것이 있으면이 도움말을 통해 도움을 받아야합니다. 원래 게시물을 편집하고 알려 주시기 바랍니다. –