[英]what is python equivalent of getInputStream() and getOutputStream()?
我有用Java编写的代码,因为我不知道Java想使用python建立套接字连接,但是我无法理解python等效于Java的getInputStream()和getOutputStream()吗? 以及如何在python中使用它? 接下来,您可以找到我已经在python中编写的代码。
import socket
try:
resMsg = None
myClient = None
myClient = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip = socket.gethostbyname("usddcwvtrkjxts3")
#print(ip)
port = 3450
address = (ip, port)
myClient.connect(address)
data = myClient.recv(1024)
print(data)
except:
print("connection failed")
我已经用Java编写了以下代码:
public String invoke(String requestMsg) {
String resMsg = null;
Socket myClient = null;
DataInputStream input = null;
PrintWriter output = null;
try {
try {
myClient = new Socket("vwddtrkjxts002", 3450);
input = new DataInputStream(myClient.getInputStream());
output = new java.io.PrintWriter(myClient.getOutputStream(), true);
output.println(requestMsg);
output.write("\u001A");
output.flush();
StringBuffer message = new StringBuffer();
try {
while (true) {
message.append((char) input.readByte());
}
} catch (EOFException e1) {
}
resMsg = message.toString();
System.out.println(resMsg);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
input.close();
myClient.close();
} catch (IOException e) {
System.out.println(e);
}
}
return resMsg;
}
我能够与python建立连接,但无法将任何内容打印为输出。
任何建议都会有所帮助! 先感谢您
你可以在这里查看一个好的教程
综上所述,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.