Python for Ethical Hackers
Mohammad reza Kamalifard
Kamalifard@datasec.ir
Python Language Essentials
Module 3 : Network Security
Part 2 :
SocketServer Framework
Mohammad reza Kamalifard
Kamalifard...
Python Language Essentials
Module 3 : Network Security
Part 2 :
SocketServer Framework
Mohammad reza Kamalifard
Kamalifard...
Client side
import socket
import sys
if len(sys.argv) < 3 :
print 'Please Enter address and port'
exit()
tcp_socket = sock...
-----Client-----
python client.py 127.0.0.1 8000
Please Enter a Message! : Salam
Server Send back : Salam
Please Enter a M...
SocketServer Framework
Framework in Python to create TCP and UDP servers
Does all the basic steps for you in the backgroun...
SocketServer Framework
Has to be Subclass of BaseRequestHandler
Override handle() to process request
Call handle_request o...
import SocketServer
class EchoHandler(SocketServer.BaseRequestHandler):
def handle(self):
print 'Got Connection from : ', ...
Exercise
is this server multi_threaded ?
code up the multi_threaded version of the SocketServer
Mohammad reza Kamalifard
K...
This work is licensed under the Creative Commons
Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license...
Upcoming SlideShare
Loading in …5
×

اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی

429 views
391 views

Published on

Python for ethical hackers by Mohammad reza kamalifard
در این جلسه به بررسی ماژول
SocketServer
در پایتون می‌پردازدیم

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
429
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی

  1. 1. Python for Ethical Hackers Mohammad reza Kamalifard Kamalifard@datasec.ir
  2. 2. Python Language Essentials Module 3 : Network Security Part 2 : SocketServer Framework Mohammad reza Kamalifard Kamalifard.ir/pysec101
  3. 3. Python Language Essentials Module 3 : Network Security Part 2 : SocketServer Framework Mohammad reza Kamalifard Kamalifard.ir/pysec101
  4. 4. Client side import socket import sys if len(sys.argv) < 3 : print 'Please Enter address and port' exit() tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcp_socket.connect((sys.argv[1], int(sys.argv[2]))) while True: userInput = raw_input('Please Enter a Message! : ') tcp_socket.send(userInput) print 'Server Send back : ' + str(tcp_socket.recv(2048)) tcp_socket.close() Mohammad reza Kamalifard Kamalifard.ir/pysec101
  5. 5. -----Client----- python client.py 127.0.0.1 8000 Please Enter a Message! : Salam Server Send back : Salam Please Enter a Message! : WELCOME TO PYSEC101! Server Send back : WELCOME TO PYSEC101! Please Enter a Message! : -----Server----- Waiting for client ... Revived connection from : 127.0.0.1 Starting ECHO output... Client send : Salam Client send : WELCOME TO PYSEC101! Client send : Closing Connection Mohammad reza Kamalifard Kamalifard.ir/pysec101
  6. 6. SocketServer Framework Framework in Python to create TCP and UDP servers Does all the basic steps for you in the background Comes in handy if you want to create a server to lure a client and analyze its behavior Mohammad reza Kamalifard Kamalifard.ir/pysec101
  7. 7. SocketServer Framework Has to be Subclass of BaseRequestHandler Override handle() to process request Call handle_request or server_forever to process clients For TCP Servers self.request is the client socket self.client_addresss is the client details Mohammad reza Kamalifard Kamalifard.ir/pysec101
  8. 8. import SocketServer class EchoHandler(SocketServer.BaseRequestHandler): def handle(self): print 'Got Connection from : ', self.client_address data = 'dummy' while len(data): data = self.request.recv(1024) print 'Client sent :' + data self.request.send(data) print 'client left' server_address = ('127.0.0.1', 9050) server = SocketServer.TCPServer(server_address, EchoHandler) server.serve_forever() Mohammad reza Kamalifard Kamalifard.ir/pysec101
  9. 9. Exercise is this server multi_threaded ? code up the multi_threaded version of the SocketServer Mohammad reza Kamalifard Kamalifard.ir/pysec101
  10. 10. This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad reza Kamalifard. All rights reserved.

×