Skip to content

Commit c32ba4d

Browse files
authored
Testing network connection using socket programming
1 parent ec308c2 commit c32ba4d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tests/client-server/socket-test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Testing network connection to Google using socket programming
2+
3+
import socket # for socket
4+
import sys
5+
6+
try:
7+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8+
print ("Socket successfully created")
9+
except socket.error as err:
10+
print ("socket creation failed with error %s" %(err))
11+
12+
# default port for socket
13+
port = 80
14+
15+
try:
16+
host_ip = socket.gethostbyname('www.google.com')
17+
except socket.gaierror:
18+
19+
# this means could not resolve the host
20+
print ("there was an error resolving the host")
21+
sys.exit()
22+
23+
# connecting to the server
24+
s.connect((host_ip, port))
25+
26+
print ("the socket has successfully connected to google")

0 commit comments

Comments
 (0)