-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_module.py
More file actions
102 lines (52 loc) · 2.44 KB
/
Copy pathsocket_module.py
File metadata and controls
102 lines (52 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#----- A simple server-client program -----#
#bind() ### which binds it to a specific ip and port so that it can listen to incoming requests on that ip and port.
#listen() ### which puts the server into listening mode.
#accept() ### initiates a connection with the client.
#close() ### closes the connection with the client.
#-------------------------#
### connecting to a server:
# ping www.google.com
### finding the ip using python:
import socket
ip = socket.gethostbyname("www.google.com")
print(ip)
##################################
import socket ## importing the socket module
s = socket.socket() ## creating a socket object
port = 12345 ## define the port on which you want to connect
s.connect('127.0.0.1', port) ## connect to the server on local computer
print(s.recv(1024).decode()) ## receive data from the server and decoding to get the string.
s.close() ## close the connection
#===============================================
### A simple server-client program
import socket
s = socket.socket() ## creating a socket object
print("Socket successfully craeted")
port = 12345 ## reserve a port
s.bind('', port) ## bind to the port, we have not types any ip in teh ip field instead we have inputted an empty string this makes the server listen to requests coming from other computers on the network.
print("Socket binded to %s" %(port))
s.listen(5) ## putting the socket in listening mode
print("Socket is listening")
while True:
c, addr = s.accept() ## establish connection with client
print("Got connection from", addr)
c.send("Thank you for connecting".encode()) ## sending a thank you message to the client. encoding to see byte type.
c.close() ## closing the connection with the client
break ## breaking once connection closed
#===============================================
### script for cennecting to google using socket
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket successfully cerated")
except socket.error as err:
print("Socket creation is failes with error %s" %(err))
port = 80 ## default port for socket
try:
host_ip = socket.gethostbyname("www.google.com")
except socket.gaierror:
print("Thre was an error resolving the host") ## this means cold not resolve the host
sys.exit()
s.connect((host_ip, port)) ## connecting to the server
print("The socket has successfully connected to google")