Python tutorials are widely used in computer languages. The following articles describe how to use Python tutorials. If you need this practical application technique, simply read the following articles, I hope you will be rewarded with the introduction in this article.
Test whether the computers in the LAN are connected. The ip addresses of these computers range from 192.168.0.101 to 192.168.0.200.
Idea: use shell programming (Linux is usually bash and Windows is a batch processing script ).
For example, in Windows, ping the ip command to test each machine in sequence and obtain the console output. the console text is usually "Reply from... "The text is" time out... ", so you can find the string in the result to know whether the machine is connected.
Implementation: The Java code is as follows:
- String cmd="cmd.exe ping ";
- String ipprefix="192.168.10.";
- int begin=101;
- int end=200;
- Process p=null;
- for(int i=begin;i<end;i++){
- p= Runtime.getRuntime().exec(cmd+i);
- String line = null;
- BufferedReader reader = new BufferedReader(new
InputStreamReader(p.getInputStream()));
- while((line = reader.readLine()) != null)
- {
- //Handling line , may logs it.
- }
- reader.close();
- p.destroy();
- }
- import subprocess
- cmd="cmd.exe"
- begin=101
- end=200
- while begin<end:
- p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,
- stdin=subprocess.PIPE,
- stderr=subprocess.PIPE)
- p.stdin.write("ping 192.168.1."+str(begin)+"\n")
- p.stdin.close()
- p.wait()
- print "execution result: %s"%p.stdout.read()
-
This code runs well. The problem is that you need to do some additional work to run this code. these additional tasks include writing a class file and writing a main method to compile it into byte code.
Because the byte code cannot be run directly, you need to write a little bat or bash script to run it.
Of course, C/C ++ can do the same job. however, C/C ++ is not a cross-platform language. in this simple enough example, the differences between C/C ++ and Java implementations may not be seen, but in some more complex scenarios, for example, you need to record the connected information to the network database. because the network interfaces of Linux and Windows are implemented in different ways, you have to write two function versions. there is no such concern when using Java.
The same work is implemented in Python as follows:
Compared with Java, Python is more concise and faster. you do not need to write the main function, and the program can be directly run after being saved. in addition, Python is also cross-platform like Java.
Experienced C/Java programmers may argue that writing in C/Java is faster than writing in Python. this is a matter of opinion. my idea is that when you master both Java and Python, you will find that writing such programs using Python is much faster than Java. for example, when operating a local file, you only need a line of code instead of many Java stream packaging classes. various Languages have their own natural application scopes. using Python to process some short programs, similar to interactive programming with the operating system, saves the most time and effort.