简体   繁体   中英

Python script to check if android device is connected to internet

I am writing a python script to check if my android device is connected to internet. I am trying to do the following in adb shell:

  1. busybox telnet 173.194.33.97 80
  2. GET / HTTP/1.0

The output is: - HTTP/1.0 200 OK

I care only about HTTP/1.0 200 OK, to check the internet connectivity.

I tried to use netcat in python but I don't have a very good experience to implement it.

Any help will be greatly appreciated!

Finally, the Python script is:

cmd = "adb shell \"echo \'GET / HTTP/1.0\n\n\' | nc 173.194.33.103 80\""
p = Popen(shlex.split(cmd), stdout=PIPE, stderr=STDOUT)
#The output will be "HTTP/1.1 200 OK", "HTTP/1.0 200 OK" OR "HTTP/2.0 200 OK"
for line in iter(p.stdout.readline, b''):
    if line.rstrip().startswith('HTTP/') and line.rstrip().endswith('200 OK'):
        print '\nFOUND IT: ' + line.rstrip()
        wifi_flag = True
        break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM