forked from averagesecurityguy/Python-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
57 lines (45 loc) · 2 KB
/
server.py
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
#!/usr/bin/env python
#-----------------------------------------------------------------------------
# This example will setup a server on the localhost on the specified port.
# Data is received from the client, processed, and echoed back to the client.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Import any needed libraries below
#-----------------------------------------------------------------------------
# We need to use the socket library
import socket
#-----------------------------------------------------------------------------
# Place any necessary functions below
#-----------------------------------------------------------------------------
def process_data(d):
# Place any data processing code here.
return d
#-----------------------------------------------------------------------------
# Begin the main program.
#-----------------------------------------------------------------------------
# Create a new socket and specify the host and port to listen on.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '127.0.0.1'
port = 80
# Bind the socket to a port on the host. If the port is in use you will
# receive an error.
sock.bind((host,port))
# Tell the socket to start listening for connections. Will handle a maximum of
# one queued connection.
print "Listening..."
sock.listen(1)
# Put the server in a loop that accepts a connection, receives client data,
# processes the data, send the processed data to the client, and close the
# connection. The loop continues forever.
while True:
# Accept a connection and receive data
client, address = sock.accept()
data = client.recv(1024)
print "Received <- %s" % (data)
if data:
# If we received any data processes it and send it back.
send = process_data(data)
print "Sent ->: %s" % (send)
client.send(send)
# Close the client connection
client.close()