[{"data":1,"prerenderedAt":1621},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-ping-script-example":3},{"id":4,"title":5,"body":6,"description":1614,"extension":1615,"meta":1616,"navigation":92,"path":1617,"seo":1618,"stem":1619,"__hash__":1620},"content\u002Fexamples\u002Fpython-ping-script-example.md","Python Ping Script Example",{"type":7,"value":8,"toc":1585},"minimark",[9,13,22,25,41,52,57,310,320,324,327,330,353,356,360,363,377,390,394,397,604,607,660,664,671,691,694,697,725,731,737,792,795,798,812,830,836,850,853,859,865,879,882,888,894,912,919,925,929,935,938,945,948,954,957,968,971,975,978,1003,1007,1027,1031,1034,1037,1054,1057,1266,1274,1278,1281,1301,1304,1339,1342,1346,1349,1402,1405,1438,1458,1462,1466,1474,1483,1489,1502,1506,1509,1513,1516,1529,1533,1536,1539,1553,1557,1578,1581],[10,11,5],"h1",{"id":12},"python-ping-script-example",[14,15,16,17,21],"p",{},"This beginner-friendly example shows how to make a simple Python ping script by running your operating system's ",[18,19,20],"code",{},"ping"," command.",[14,23,24],{},"This is useful when you want to:",[26,27,28,32,35,38],"ul",{},[29,30,31],"li",{},"check whether a host can be reached",[29,33,34],{},"test basic network connectivity",[29,36,37],{},"learn how Python can run terminal commands",[29,39,40],{},"build a small troubleshooting script",[14,42,43,44,47,48,51],{},"Python does not have a simple built-in ",[18,45,46],{},"ping()"," function for this. A common beginner approach is to run the system command with ",[18,49,50],{},"subprocess",".",[53,54,56],"h2",{"id":55},"quick-example","Quick example",[58,59,64],"pre",{"className":60,"code":61,"language":62,"meta":63,"style":63},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import platform\nimport subprocess\n\nhost = \"google.com\"\nparam = \"-n\" if platform.system().lower() == \"windows\" else \"-c\"\nresult = subprocess.run([\"ping\", param, \"4\", host], capture_output=True, text=True)\n\nprint(result.stdout)\nprint(\"Reachable\" if result.returncode == 0 else \"Not reachable\")\n","python","",[18,65,66,79,87,94,115,173,243,248,269],{"__ignoreMap":63},[67,68,71,75],"span",{"class":69,"line":70},"line",1,[67,72,74],{"class":73},"sVHd0","import",[67,76,78],{"class":77},"su5hD"," platform\n",[67,80,82,84],{"class":69,"line":81},2,[67,83,74],{"class":73},[67,85,86],{"class":77}," subprocess\n",[67,88,90],{"class":69,"line":89},3,[67,91,93],{"emptyLinePlaceholder":92},true,"\n",[67,95,97,100,104,108,112],{"class":69,"line":96},4,[67,98,99],{"class":77},"host ",[67,101,103],{"class":102},"smGrS","=",[67,105,107],{"class":106},"sjJ54"," \"",[67,109,111],{"class":110},"s_sjI","google.com",[67,113,114],{"class":106},"\"\n",[67,116,118,121,123,125,128,131,134,137,140,144,147,150,153,156,158,161,163,166,168,171],{"class":69,"line":117},5,[67,119,120],{"class":77},"param ",[67,122,103],{"class":102},[67,124,107],{"class":106},[67,126,127],{"class":110},"-n",[67,129,130],{"class":106},"\"",[67,132,133],{"class":73}," if",[67,135,136],{"class":77}," platform",[67,138,51],{"class":139},"sP7_E",[67,141,143],{"class":142},"slqww","system",[67,145,146],{"class":139},"().",[67,148,149],{"class":142},"lower",[67,151,152],{"class":139},"()",[67,154,155],{"class":102}," ==",[67,157,107],{"class":106},[67,159,160],{"class":110},"windows",[67,162,130],{"class":106},[67,164,165],{"class":73}," else",[67,167,107],{"class":106},[67,169,170],{"class":110},"-c",[67,172,114],{"class":106},[67,174,176,179,181,184,186,189,192,194,196,198,201,204,206,208,211,213,215,218,221,225,227,231,233,236,238,240],{"class":69,"line":175},6,[67,177,178],{"class":77},"result ",[67,180,103],{"class":102},[67,182,183],{"class":77}," subprocess",[67,185,51],{"class":139},[67,187,188],{"class":142},"run",[67,190,191],{"class":139},"([",[67,193,130],{"class":106},[67,195,20],{"class":110},[67,197,130],{"class":106},[67,199,200],{"class":139},",",[67,202,203],{"class":142}," param",[67,205,200],{"class":139},[67,207,107],{"class":106},[67,209,210],{"class":110},"4",[67,212,130],{"class":106},[67,214,200],{"class":139},[67,216,217],{"class":142}," host",[67,219,220],{"class":139},"],",[67,222,224],{"class":223},"s99_P"," capture_output",[67,226,103],{"class":102},[67,228,230],{"class":229},"s39Yj","True",[67,232,200],{"class":139},[67,234,235],{"class":223}," text",[67,237,103],{"class":102},[67,239,230],{"class":229},[67,241,242],{"class":139},")\n",[67,244,246],{"class":69,"line":245},7,[67,247,93],{"emptyLinePlaceholder":92},[67,249,251,255,258,261,263,267],{"class":69,"line":250},8,[67,252,254],{"class":253},"sptTA","print",[67,256,257],{"class":139},"(",[67,259,260],{"class":142},"result",[67,262,51],{"class":139},[67,264,266],{"class":265},"skxfh","stdout",[67,268,242],{"class":139},[67,270,272,274,276,278,281,283,285,288,290,293,295,299,301,303,306,308],{"class":69,"line":271},9,[67,273,254],{"class":253},[67,275,257],{"class":139},[67,277,130],{"class":106},[67,279,280],{"class":110},"Reachable",[67,282,130],{"class":106},[67,284,133],{"class":73},[67,286,287],{"class":142}," result",[67,289,51],{"class":139},[67,291,292],{"class":265},"returncode",[67,294,155],{"class":102},[67,296,298],{"class":297},"srdBf"," 0",[67,300,165],{"class":73},[67,302,107],{"class":106},[67,304,305],{"class":110},"Not reachable",[67,307,130],{"class":106},[67,309,242],{"class":139},[14,311,312,316,317,319],{},[313,314,315],"strong",{},"Note:"," This uses the operating system ",[18,318,20],{}," command. It works as a simple beginner script, but the exact output depends on your system.",[53,321,323],{"id":322},"what-this-script-does","What this script does",[14,325,326],{},"This script checks whether a host can be reached over the network.",[14,328,329],{},"It does that by:",[26,331,332,338,344,347,350],{},[29,333,334,335,337],{},"choosing the correct ",[18,336,20],{}," option for your operating system",[29,339,340,341,343],{},"running the external ",[18,342,20],{}," command",[29,345,346],{},"collecting the command output",[29,348,349],{},"checking the return code",[29,351,352],{},"printing a simple result message",[14,354,355],{},"This is a good example for learning and small utility scripts. It is not a full monitoring system.",[53,357,359],{"id":358},"when-to-use-a-python-ping-script","When to use a Python ping script",[14,361,362],{},"A script like this is useful when you want to:",[26,364,365,368,371,374],{},[29,366,367],{},"check if a website or device responds",[29,369,370],{},"test network reachability in a simple script",[29,372,373],{},"learn how to run terminal commands from Python",[29,375,376],{},"build small monitoring or troubleshooting tools",[14,378,379,380,385,386,51],{},"If you want more practice with scripts that run checks and report results, see the ",[381,382,384],"a",{"href":383},"\u002Fexamples\u002Fpython-url-checker-script-example\u002F","Python URL checker script example"," or a ",[381,387,389],{"href":388},"\u002Fexamples\u002Fpython-command-line-tool-example\u002F","Python command-line tool example",[53,391,393],{"id":392},"how-the-example-works","How the example works",[14,395,396],{},"Here is the example again:",[58,398,400],{"className":60,"code":399,"language":62,"meta":63,"style":63},"import platform\nimport subprocess\n\nhost = \"google.com\"\nparam = \"-n\" if platform.system().lower() == \"windows\" else \"-c\"\n\nresult = subprocess.run(\n    [\"ping\", param, \"4\", host],\n    capture_output=True,\n    text=True\n)\n\nprint(result.stdout)\nprint(\"Reachable\" if result.returncode == 0 else \"Not reachable\")\n",[18,401,402,408,414,418,430,472,476,491,521,533,544,549,554,569],{"__ignoreMap":63},[67,403,404,406],{"class":69,"line":70},[67,405,74],{"class":73},[67,407,78],{"class":77},[67,409,410,412],{"class":69,"line":81},[67,411,74],{"class":73},[67,413,86],{"class":77},[67,415,416],{"class":69,"line":89},[67,417,93],{"emptyLinePlaceholder":92},[67,419,420,422,424,426,428],{"class":69,"line":96},[67,421,99],{"class":77},[67,423,103],{"class":102},[67,425,107],{"class":106},[67,427,111],{"class":110},[67,429,114],{"class":106},[67,431,432,434,436,438,440,442,444,446,448,450,452,454,456,458,460,462,464,466,468,470],{"class":69,"line":117},[67,433,120],{"class":77},[67,435,103],{"class":102},[67,437,107],{"class":106},[67,439,127],{"class":110},[67,441,130],{"class":106},[67,443,133],{"class":73},[67,445,136],{"class":77},[67,447,51],{"class":139},[67,449,143],{"class":142},[67,451,146],{"class":139},[67,453,149],{"class":142},[67,455,152],{"class":139},[67,457,155],{"class":102},[67,459,107],{"class":106},[67,461,160],{"class":110},[67,463,130],{"class":106},[67,465,165],{"class":73},[67,467,107],{"class":106},[67,469,170],{"class":110},[67,471,114],{"class":106},[67,473,474],{"class":69,"line":175},[67,475,93],{"emptyLinePlaceholder":92},[67,477,478,480,482,484,486,488],{"class":69,"line":245},[67,479,178],{"class":77},[67,481,103],{"class":102},[67,483,183],{"class":77},[67,485,51],{"class":139},[67,487,188],{"class":142},[67,489,490],{"class":139},"(\n",[67,492,493,496,498,500,502,504,506,508,510,512,514,516,518],{"class":69,"line":250},[67,494,495],{"class":139},"    [",[67,497,130],{"class":106},[67,499,20],{"class":110},[67,501,130],{"class":106},[67,503,200],{"class":139},[67,505,203],{"class":142},[67,507,200],{"class":139},[67,509,107],{"class":106},[67,511,210],{"class":110},[67,513,130],{"class":106},[67,515,200],{"class":139},[67,517,217],{"class":142},[67,519,520],{"class":139},"],\n",[67,522,523,526,528,530],{"class":69,"line":271},[67,524,525],{"class":223},"    capture_output",[67,527,103],{"class":102},[67,529,230],{"class":229},[67,531,532],{"class":139},",\n",[67,534,536,539,541],{"class":69,"line":535},10,[67,537,538],{"class":223},"    text",[67,540,103],{"class":102},[67,542,543],{"class":229},"True\n",[67,545,547],{"class":69,"line":546},11,[67,548,242],{"class":139},[67,550,552],{"class":69,"line":551},12,[67,553,93],{"emptyLinePlaceholder":92},[67,555,557,559,561,563,565,567],{"class":69,"line":556},13,[67,558,254],{"class":253},[67,560,257],{"class":139},[67,562,260],{"class":142},[67,564,51],{"class":139},[67,566,266],{"class":265},[67,568,242],{"class":139},[67,570,572,574,576,578,580,582,584,586,588,590,592,594,596,598,600,602],{"class":69,"line":571},14,[67,573,254],{"class":253},[67,575,257],{"class":139},[67,577,130],{"class":106},[67,579,280],{"class":110},[67,581,130],{"class":106},[67,583,133],{"class":73},[67,585,287],{"class":142},[67,587,51],{"class":139},[67,589,292],{"class":265},[67,591,155],{"class":102},[67,593,298],{"class":297},[67,595,165],{"class":73},[67,597,107],{"class":106},[67,599,305],{"class":110},[67,601,130],{"class":106},[67,603,242],{"class":139},[14,605,606],{},"Step by step, the script does this:",[608,609,610,617,622,628,637,645,650,653],"ol",{},[29,611,612,613,616],{},"Imports ",[18,614,615],{},"platform"," to detect the operating system.",[29,618,612,619,621],{},[18,620,50],{}," to run an external command.",[29,623,624,625,51],{},"Stores the target host in ",[18,626,627],{},"host",[29,629,630,631,633,634,636],{},"Uses ",[18,632,127],{}," on Windows and ",[18,635,170],{}," on macOS\u002FLinux.",[29,638,639,640,642,643,51],{},"Runs ",[18,641,20],{}," with a count of ",[18,644,210],{},[29,646,647,648,51],{},"Saves the result in ",[18,649,260],{},[29,651,652],{},"Prints the command output.",[29,654,655,656,659],{},"Checks ",[18,657,658],{},"result.returncode"," to decide whether the host is reachable.",[53,661,663],{"id":662},"key-lines-to-explain","Key lines to explain",[665,666,668],"h3",{"id":667},"platformsystemlower",[18,669,670],{},"platform.system().lower()",[58,672,674],{"className":60,"code":673,"language":62,"meta":63,"style":63},"platform.system().lower()\n",[18,675,676],{"__ignoreMap":63},[67,677,678,680,682,684,686,688],{"class":69,"line":70},[67,679,615],{"class":77},[67,681,51],{"class":139},[67,683,143],{"class":142},[67,685,146],{"class":139},[67,687,149],{"class":142},[67,689,690],{"class":139},"()\n",[14,692,693],{},"This checks the current operating system and turns it into lowercase text.",[14,695,696],{},"Examples:",[26,698,699,708,716],{},[29,700,701,704,705],{},[18,702,703],{},"\"Windows\""," becomes ",[18,706,707],{},"\"windows\"",[29,709,710,704,713],{},[18,711,712],{},"\"Linux\"",[18,714,715],{},"\"linux\"",[29,717,718,704,721,724],{},[18,719,720],{},"\"Darwin\"",[18,722,723],{},"\"darwin\""," on macOS",[14,726,727,728,730],{},"That helps the script choose the correct ",[18,729,20],{}," option.",[665,732,734],{"id":733},"subprocessrun",[18,735,736],{},"subprocess.run(...)",[58,738,740],{"className":60,"code":739,"language":62,"meta":63,"style":63},"subprocess.run([\"ping\", param, \"4\", host], capture_output=True, text=True)\n",[18,741,742],{"__ignoreMap":63},[67,743,744,746,748,750,752,754,756,758,760,762,764,766,768,770,772,774,776,778,780,782,784,786,788,790],{"class":69,"line":70},[67,745,50],{"class":77},[67,747,51],{"class":139},[67,749,188],{"class":142},[67,751,191],{"class":139},[67,753,130],{"class":106},[67,755,20],{"class":110},[67,757,130],{"class":106},[67,759,200],{"class":139},[67,761,203],{"class":142},[67,763,200],{"class":139},[67,765,107],{"class":106},[67,767,210],{"class":110},[67,769,130],{"class":106},[67,771,200],{"class":139},[67,773,217],{"class":142},[67,775,220],{"class":139},[67,777,224],{"class":223},[67,779,103],{"class":102},[67,781,230],{"class":229},[67,783,200],{"class":139},[67,785,235],{"class":223},[67,787,103],{"class":102},[67,789,230],{"class":229},[67,791,242],{"class":139},[14,793,794],{},"This runs an external command from Python.",[14,796,797],{},"In this case, it runs:",[26,799,800,806],{},[29,801,802,805],{},[18,803,804],{},"ping -n 4 google.com"," on Windows",[29,807,808,811],{},[18,809,810],{},"ping -c 4 google.com"," on Linux or macOS",[14,813,814,815,822,823,829],{},"If you are new to running system commands from Python, the ",[381,816,818,821],{"href":817},"\u002Fstandard-library\u002Fpython-os-module-overview\u002F",[18,819,820],{},"os"," module overview"," and ",[381,824,826,821],{"href":825},"\u002Fstandard-library\u002Fpython-sys-module-overview\u002F",[18,827,828],{},"sys"," are also useful background topics.",[665,831,833],{"id":832},"capture_outputtrue",[18,834,835],{},"capture_output=True",[58,837,839],{"className":60,"code":838,"language":62,"meta":63,"style":63},"capture_output=True\n",[18,840,841],{"__ignoreMap":63},[67,842,843,846,848],{"class":69,"line":70},[67,844,845],{"class":77},"capture_output",[67,847,103],{"class":102},[67,849,543],{"class":229},[14,851,852],{},"This collects the command output so Python can use it later.",[14,854,855,856,51],{},"Without this, the command may print directly to the terminal instead of being stored in ",[18,857,858],{},"result.stdout",[665,860,862],{"id":861},"texttrue",[18,863,864],{},"text=True",[58,866,868],{"className":60,"code":867,"language":62,"meta":63,"style":63},"text=True\n",[18,869,870],{"__ignoreMap":63},[67,871,872,875,877],{"class":69,"line":70},[67,873,874],{"class":77},"text",[67,876,103],{"class":102},[67,878,543],{"class":229},[14,880,881],{},"This tells Python to return the output as normal text.",[14,883,884,885,887],{},"Without ",[18,886,864],{},", the output would be returned as bytes, which is harder for beginners to read.",[665,889,891],{"id":890},"resultreturncode-0",[18,892,893],{},"result.returncode == 0",[58,895,897],{"className":60,"code":896,"language":62,"meta":63,"style":63},"result.returncode == 0\n",[18,898,899],{"__ignoreMap":63},[67,900,901,903,905,907,909],{"class":69,"line":70},[67,902,260],{"class":77},[67,904,51],{"class":139},[67,906,292],{"class":265},[67,908,155],{"class":102},[67,910,911],{"class":297}," 0\n",[14,913,914,915,918],{},"A return code of ",[18,916,917],{},"0"," usually means the command succeeded.",[14,920,921,922,924],{},"For this script, that usually means the ",[18,923,20],{}," command was able to reach the host.",[53,926,928],{"id":927},"expected-output","Expected output",[14,930,931,932,934],{},"The output will usually show several lines from the ",[18,933,20],{}," command, followed by a final message.",[14,936,937],{},"Example:",[58,939,943],{"className":940,"code":942,"language":874,"meta":63},[941],"language-text","PING google.com (142.250.190.14): 56 data bytes\n64 bytes from 142.250.190.14: icmp_seq=0 ttl=117 time=14.2 ms\n64 bytes from 142.250.190.14: icmp_seq=1 ttl=117 time=13.8 ms\n64 bytes from 142.250.190.14: icmp_seq=2 ttl=117 time=14.0 ms\n64 bytes from 142.250.190.14: icmp_seq=3 ttl=117 time=13.9 ms\n\nReachable\n",[18,944,942],{"__ignoreMap":63},[14,946,947],{},"If the host cannot be reached, you may see different output and this final line:",[58,949,952],{"className":950,"code":951,"language":874,"meta":63},[941],"Not reachable\n",[18,953,951],{"__ignoreMap":63},[14,955,956],{},"The exact format is different on:",[26,958,959,962,965],{},[29,960,961],{},"Windows",[29,963,964],{},"Linux",[29,966,967],{},"macOS",[14,969,970],{},"That is normal.",[53,972,974],{"id":973},"common-beginner-problems","Common beginner problems",[14,976,977],{},"These are the most common reasons a simple ping script does not work as expected:",[26,979,980,988,991,994,1000],{},[29,981,982,983,985,986],{},"using the wrong flag: ",[18,984,127],{}," vs ",[18,987,170],{},[29,989,990],{},"trying to ping a host with no internet connection",[29,992,993],{},"firewall or network rules blocking ping",[29,995,996,997,999],{},"the ",[18,998,20],{}," command not being available in the current environment",[29,1001,1002],{},"thinking ping success means the whole website or app is working",[665,1004,1006],{"id":1005},"common-causes","Common causes",[26,1008,1009,1012,1015,1018,1021,1024],{},[29,1010,1011],{},"Wrong ping flag for the operating system",[29,1013,1014],{},"Host name is misspelled",[29,1016,1017],{},"No network connection",[29,1019,1020],{},"Ping blocked by firewall",[29,1022,1023],{},"Running in a restricted environment where ping is unavailable",[29,1025,1026],{},"Assuming return code meanings without checking system behavior",[53,1028,1030],{"id":1029},"simple-improvements","Simple improvements",[14,1032,1033],{},"Once the basic version works, you can improve it in small steps.",[14,1035,1036],{},"For example, you can:",[26,1038,1039,1042,1045,1048,1051],{},[29,1040,1041],{},"let the user enter a hostname",[29,1043,1044],{},"ping multiple hosts in a loop",[29,1046,1047],{},"save results to a file",[29,1049,1050],{},"add a timeout",[29,1052,1053],{},"show only success or failure instead of the full output",[14,1055,1056],{},"Here is a small improvement that asks the user for a host:",[58,1058,1060],{"className":60,"code":1059,"language":62,"meta":63,"style":63},"import platform\nimport subprocess\n\nhost = input(\"Enter a host to ping: \").strip()\nparam = \"-n\" if platform.system().lower() == \"windows\" else \"-c\"\n\nresult = subprocess.run(\n    [\"ping\", param, \"4\", host],\n    capture_output=True,\n    text=True\n)\n\nprint(result.stdout)\nprint(\"Reachable\" if result.returncode == 0 else \"Not reachable\")\n",[18,1061,1062,1068,1074,1078,1104,1146,1150,1164,1192,1202,1210,1214,1218,1232],{"__ignoreMap":63},[67,1063,1064,1066],{"class":69,"line":70},[67,1065,74],{"class":73},[67,1067,78],{"class":77},[67,1069,1070,1072],{"class":69,"line":81},[67,1071,74],{"class":73},[67,1073,86],{"class":77},[67,1075,1076],{"class":69,"line":89},[67,1077,93],{"emptyLinePlaceholder":92},[67,1079,1080,1082,1084,1087,1089,1091,1094,1096,1099,1102],{"class":69,"line":96},[67,1081,99],{"class":77},[67,1083,103],{"class":102},[67,1085,1086],{"class":253}," input",[67,1088,257],{"class":139},[67,1090,130],{"class":106},[67,1092,1093],{"class":110},"Enter a host to ping: ",[67,1095,130],{"class":106},[67,1097,1098],{"class":139},").",[67,1100,1101],{"class":142},"strip",[67,1103,690],{"class":139},[67,1105,1106,1108,1110,1112,1114,1116,1118,1120,1122,1124,1126,1128,1130,1132,1134,1136,1138,1140,1142,1144],{"class":69,"line":117},[67,1107,120],{"class":77},[67,1109,103],{"class":102},[67,1111,107],{"class":106},[67,1113,127],{"class":110},[67,1115,130],{"class":106},[67,1117,133],{"class":73},[67,1119,136],{"class":77},[67,1121,51],{"class":139},[67,1123,143],{"class":142},[67,1125,146],{"class":139},[67,1127,149],{"class":142},[67,1129,152],{"class":139},[67,1131,155],{"class":102},[67,1133,107],{"class":106},[67,1135,160],{"class":110},[67,1137,130],{"class":106},[67,1139,165],{"class":73},[67,1141,107],{"class":106},[67,1143,170],{"class":110},[67,1145,114],{"class":106},[67,1147,1148],{"class":69,"line":175},[67,1149,93],{"emptyLinePlaceholder":92},[67,1151,1152,1154,1156,1158,1160,1162],{"class":69,"line":245},[67,1153,178],{"class":77},[67,1155,103],{"class":102},[67,1157,183],{"class":77},[67,1159,51],{"class":139},[67,1161,188],{"class":142},[67,1163,490],{"class":139},[67,1165,1166,1168,1170,1172,1174,1176,1178,1180,1182,1184,1186,1188,1190],{"class":69,"line":250},[67,1167,495],{"class":139},[67,1169,130],{"class":106},[67,1171,20],{"class":110},[67,1173,130],{"class":106},[67,1175,200],{"class":139},[67,1177,203],{"class":142},[67,1179,200],{"class":139},[67,1181,107],{"class":106},[67,1183,210],{"class":110},[67,1185,130],{"class":106},[67,1187,200],{"class":139},[67,1189,217],{"class":142},[67,1191,520],{"class":139},[67,1193,1194,1196,1198,1200],{"class":69,"line":271},[67,1195,525],{"class":223},[67,1197,103],{"class":102},[67,1199,230],{"class":229},[67,1201,532],{"class":139},[67,1203,1204,1206,1208],{"class":69,"line":535},[67,1205,538],{"class":223},[67,1207,103],{"class":102},[67,1209,543],{"class":229},[67,1211,1212],{"class":69,"line":546},[67,1213,242],{"class":139},[67,1215,1216],{"class":69,"line":551},[67,1217,93],{"emptyLinePlaceholder":92},[67,1219,1220,1222,1224,1226,1228,1230],{"class":69,"line":556},[67,1221,254],{"class":253},[67,1223,257],{"class":139},[67,1225,260],{"class":142},[67,1227,51],{"class":139},[67,1229,266],{"class":265},[67,1231,242],{"class":139},[67,1233,1234,1236,1238,1240,1242,1244,1246,1248,1250,1252,1254,1256,1258,1260,1262,1264],{"class":69,"line":571},[67,1235,254],{"class":253},[67,1237,257],{"class":139},[67,1239,130],{"class":106},[67,1241,280],{"class":110},[67,1243,130],{"class":106},[67,1245,133],{"class":73},[67,1247,287],{"class":142},[67,1249,51],{"class":139},[67,1251,292],{"class":265},[67,1253,155],{"class":102},[67,1255,298],{"class":297},[67,1257,165],{"class":73},[67,1259,107],{"class":106},[67,1261,305],{"class":110},[67,1263,130],{"class":106},[67,1265,242],{"class":139},[14,1267,1268,1269,1273],{},"If you want to make this more robust, learn ",[381,1270,1272],{"href":1271},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",". That will help when a command is missing or input is invalid.",[53,1275,1277],{"id":1276},"safety-and-portability-notes","Safety and portability notes",[14,1279,1280],{},"This example is simple on purpose, but a few good habits matter:",[26,1282,1283,1288,1295,1298],{},[29,1284,1285,1286],{},"Prefer passing a list to ",[18,1287,736],{},[29,1289,1290,1291,1294],{},"Avoid ",[18,1292,1293],{},"shell=True"," for simple cases",[29,1296,1297],{},"Remember that command behavior depends on the operating system",[29,1299,1300],{},"Treat this as a basic example, not a full monitoring solution",[14,1302,1303],{},"Passing a list like this is safer:",[58,1305,1307],{"className":60,"code":1306,"language":62,"meta":63,"style":63},"[\"ping\", param, \"4\", host]\n",[18,1308,1309],{"__ignoreMap":63},[67,1310,1311,1314,1316,1318,1320,1322,1324,1326,1328,1330,1332,1334,1336],{"class":69,"line":70},[67,1312,1313],{"class":139},"[",[67,1315,130],{"class":106},[67,1317,20],{"class":110},[67,1319,130],{"class":106},[67,1321,200],{"class":139},[67,1323,203],{"class":77},[67,1325,200],{"class":139},[67,1327,107],{"class":106},[67,1329,210],{"class":110},[67,1331,130],{"class":106},[67,1333,200],{"class":139},[67,1335,217],{"class":77},[67,1337,1338],{"class":139},"]\n",[14,1340,1341],{},"That is better than building one shell string yourself.",[53,1343,1345],{"id":1344},"debugging-commands","Debugging commands",[14,1347,1348],{},"If your script is not working, these commands can help you test the environment:",[58,1350,1354],{"className":1351,"code":1352,"language":1353,"meta":63,"style":63},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python ping_script.py\nping google.com\nping -c 4 google.com\nping -n 4 google.com\npython --version\n","bash",[18,1355,1356,1364,1371,1384,1395],{"__ignoreMap":63},[67,1357,1358,1361],{"class":69,"line":70},[67,1359,62],{"class":1360},"sbgvK",[67,1362,1363],{"class":110}," ping_script.py\n",[67,1365,1366,1368],{"class":69,"line":81},[67,1367,20],{"class":1360},[67,1369,1370],{"class":110}," google.com\n",[67,1372,1373,1375,1379,1382],{"class":69,"line":89},[67,1374,20],{"class":1360},[67,1376,1378],{"class":1377},"stzsN"," -c",[67,1380,1381],{"class":297}," 4",[67,1383,1370],{"class":110},[67,1385,1386,1388,1391,1393],{"class":69,"line":96},[67,1387,20],{"class":1360},[67,1389,1390],{"class":1377}," -n",[67,1392,1381],{"class":297},[67,1394,1370],{"class":110},[67,1396,1397,1399],{"class":69,"line":117},[67,1398,62],{"class":1360},[67,1400,1401],{"class":1377}," --version\n",[14,1403,1404],{},"Use them like this:",[26,1406,1407,1414,1423,1428,1432],{},[29,1408,1409,1410,1413],{},"run ",[18,1411,1412],{},"python ping_script.py"," to test your script",[29,1415,1409,1416,1419,1420,1422],{},[18,1417,1418],{},"ping google.com"," directly in the terminal to see if ",[18,1421,20],{}," works at all",[29,1424,1425,1426,811],{},"try ",[18,1427,810],{},[29,1429,1425,1430,805],{},[18,1431,804],{},[29,1433,1409,1434,1437],{},[18,1435,1436],{},"python --version"," to confirm Python is installed",[14,1439,1440,1441,1449,1450,51],{},"If Python cannot find a file or command while you test scripts, you may also run into errors like ",[381,1442,1444,1445,1448],{"href":1443},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","FileNotFoundError: ",[67,1446,1447],{},"Errno 2"," No such file or directory"," or ",[381,1451,1453,1454,1457],{"href":1452},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix\u002F","PermissionError: ",[67,1455,1456],{},"Errno 13"," Permission denied",[53,1459,1461],{"id":1460},"faq","FAQ",[665,1463,1465],{"id":1464},"does-python-have-a-built-in-ping-function","Does Python have a built-in ping function?",[14,1467,1468,1469,1471,1472,51],{},"Not as a simple built-in function. Beginners usually run the system ",[18,1470,20],{}," command with ",[18,1473,50],{},[665,1475,1477,1478,633,1480,1482],{"id":1476},"why-does-my-script-use-n-on-windows-and-c-on-linux-or-macos","Why does my script use ",[18,1479,127],{},[18,1481,170],{}," on Linux or macOS?",[14,1484,1485,1486,1488],{},"The ",[18,1487,20],{}," command uses different options on different operating systems.",[26,1490,1491,1497],{},[29,1492,1493,1494,1496],{},"Windows uses ",[18,1495,127],{}," for count",[29,1498,1499,1500],{},"Linux and macOS commonly use ",[18,1501,170],{},[665,1503,1505],{"id":1504},"why-does-the-script-say-not-reachable-even-when-the-website-opens-in-my-browser","Why does the script say Not reachable even when the website opens in my browser?",[14,1507,1508],{},"Some servers block ping requests. A browser working does not always mean ping will work.",[665,1510,1512],{"id":1511},"can-i-ping-an-ip-address-instead-of-a-domain-name","Can I ping an IP address instead of a domain name?",[14,1514,1515],{},"Yes. You can use:",[26,1517,1518,1523],{},[29,1519,1520,1521],{},"a host name like ",[18,1522,111],{},[29,1524,1525,1526],{},"an IP address like ",[18,1527,1528],{},"8.8.8.8",[665,1530,1532],{"id":1531},"is-this-the-best-way-to-monitor-servers","Is this the best way to monitor servers?",[14,1534,1535],{},"It is fine for a beginner example.",[14,1537,1538],{},"Real monitoring tools usually need:",[26,1540,1541,1544,1547,1550],{},[29,1542,1543],{},"retries",[29,1545,1546],{},"logging",[29,1548,1549],{},"timeouts",[29,1551,1552],{},"better error handling",[53,1554,1556],{"id":1555},"see-also","See also",[26,1558,1559,1565,1569,1573],{},[29,1560,1561],{},[381,1562,1564],{"href":1563},"\u002Fhow-to\u002Fhow-to-make-an-api-request-in-python\u002F","How to make an API request in Python",[29,1566,1567],{},[381,1568,389],{"href":388},[29,1570,1571],{},[381,1572,384],{"href":383},[29,1574,1575],{},[381,1576,1577],{"href":1271},"How to handle exceptions in Python",[14,1579,1580],{},"A good next step is to turn this basic ping script into a small command-line tool that checks user-provided hosts and handles errors clearly.",[1582,1583,1584],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":63,"searchDepth":81,"depth":81,"links":1586},[1587,1588,1589,1590,1591,1598,1599,1602,1603,1604,1605,1613],{"id":55,"depth":81,"text":56},{"id":322,"depth":81,"text":323},{"id":358,"depth":81,"text":359},{"id":392,"depth":81,"text":393},{"id":662,"depth":81,"text":663,"children":1592},[1593,1594,1595,1596,1597],{"id":667,"depth":89,"text":670},{"id":733,"depth":89,"text":736},{"id":832,"depth":89,"text":835},{"id":861,"depth":89,"text":864},{"id":890,"depth":89,"text":893},{"id":927,"depth":81,"text":928},{"id":973,"depth":81,"text":974,"children":1600},[1601],{"id":1005,"depth":89,"text":1006},{"id":1029,"depth":81,"text":1030},{"id":1276,"depth":81,"text":1277},{"id":1344,"depth":81,"text":1345},{"id":1460,"depth":81,"text":1461,"children":1606},[1607,1608,1610,1611,1612],{"id":1464,"depth":89,"text":1465},{"id":1476,"depth":89,"text":1609},"Why does my script use -n on Windows and -c on Linux or macOS?",{"id":1504,"depth":89,"text":1505},{"id":1511,"depth":89,"text":1512},{"id":1531,"depth":89,"text":1532},{"id":1555,"depth":81,"text":1556},"Master python ping script example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-ping-script-example",{"title":5,"description":1614},"examples\u002Fpython-ping-script-example","A5pVRNeqHdlu8ogcKv0LpNd9e0lre26AHvL2E_NelAM",1777585477414]