Moving on to the practical
First, we will make a server-side program that offers a connection to the client and sends a message to the client. Run server1.py
:
import socket host = "192.168.0.1" #Server address port = 12345 #Port of Server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host,port)) #bind server s.listen(2) conn, addr = s.accept() print addr, "Now Connected" conn.send("Thank you for connecting") conn.close()
The preceding code is very simple; it is minimal code on the server side.
First, import the socket module and define the host and port number: 192.168.0.1
is the server's IP address. Socket.AF_INET
defines the IPv4 protocol's family. Socket.SOCK_STREAM
defines the TCP connection. The s.bind((host,port))
statement ...
Get Python: Penetration Testing for Developers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.