Client.py
import
socket # Import socket
module
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a #socket object
host = '127.0.0.1' # Get local machine name or place server IP
port = 12301 # Reserve a port for your service.
s.connect((host, port))
name=raw_input("what is your name???")
s.send(name)
print s.recv(1024)
s.close() # Close the socket when done
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a #socket object
host = '127.0.0.1' # Get local machine name or place server IP
port = 12301 # Reserve a port for your service.
s.connect((host, port))
name=raw_input("what is your name???")
s.send(name)
print s.recv(1024)
s.close() # Close the socket when done
Explanation:
1. First line import socket is used to import all socket modules in client.py
2. Create socket object=s and pass two arguments to socket function first is socket.AF_INET indicates INET family and second is socket.SOCK_STREAM which indicates TCP connection
3. If running client & server code on same machine then host is either '127.0.0.1' or 'localhost'
otherwise if client & server codes are on different machine then host= server IP address
4. Port number must be same in client.py and server.py and port>1023
5. Connect host and port using socket object s.
6. Input client name with raw_input()
7. send that name using send()
8. receive data from server in recv(1024): meaning maximum 1024 bytes can be received from server and print it.
9. Close the connection
10. # is for comments in python
Server.py
import
socket # Import socket
module
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a #socket object
host = '127.0.0.1' # Get local machine name
port = 12301 # Reserve a port for your service.
s.bind((host, port))
s.listen(3)
while True:
c,addr = s.accept()
print 'Client IP: ',addr
name=c.recv(1024)
c.send('hi '+name+ )
c.close()
s.close() # Close the socket when done
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a #socket object
host = '127.0.0.1' # Get local machine name
port = 12301 # Reserve a port for your service.
s.bind((host, port))
s.listen(3)
while True:
c,addr = s.accept()
print 'Client IP: ',addr
name=c.recv(1024)
c.send('hi '+name+ )
c.close()
s.close() # Close the socket when done
Explanation:
1. First line import socket is used to import all socket modules in client.py
2. Create socket object=s
3. If client & server code on same machine then host is either '127.0.0.1' or 'localhost'
otherwise host=' ' to accept connection from any client or client IP
4. Port number must be same in client.py and server.py and port>1023
5. Bind host and port using socket object s.
6. Next keep server in running state by while True:
7. Store IP of connected client in addr variable and print it
8. Create C object to send and receive data in between connected client
9. receive data from client in recv(1024): meaning maximum 1024 bytes can be received from client and store it in name variable
10. send reply back to client
11. Close the connection
12. Close the socket
PRACTICE QUESTIONs:
1. Write client/server program in python for accepting age from client and send "You are ____years old" message back to client from server.
2. Write client/server program in python for checking entered number even or odd
No comments:
Post a Comment