Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/613461235095457792
Added python 2 tag
Link
shuttle87
  • 2k
  • 14
  • 19
deleted 66 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Hello can someone help me with these code. II put itthis together and am having some doubts to the number of connections I can make and. I also doubt if I can brute force the server to serve up connections and then hang.

Please take a look and tell me what I can do better, suggestions etc.

Thanks!

'''
    Simple socket server using threads
'''

import socket
import sys
from thread import *


HOST = '192.168.0.102'   # Symbolic name meaning all available interfaces
PORT = 8887 # Arbitrary non-privileged port
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print 'Socket created'
 
#Bind socket to local host and port

try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
 
#Start listening on socket
s.listen(10)

print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(conn):
    
     
    #infinite loop so that function do not terminate and thread do not end.
    while True: 
        
         
        #Receiving from client
        data = conn.recv(1024)
        print data
        
        reply = 'OK...' + data + str(addr)
        if not data:
            break
     
        conn.sendall(reply)
     
    #came out of loop
    conn.close()
 
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])
     
    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))
 
s.close()

Hello can someone help me with these code. I put it together and having some doubts to the number of connections I can make and also if I can brute force the server to serve up connections and then hang.

Please take a look and tell me what I can do better, suggestions etc.

Thanks!

'''
    Simple socket server using threads
'''

import socket
import sys
from thread import *


HOST = '192.168.0.102'   # Symbolic name meaning all available interfaces
PORT = 8887 # Arbitrary non-privileged port
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print 'Socket created'
 
#Bind socket to local host and port

try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
 
#Start listening on socket
s.listen(10)

print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(conn):
    
     
    #infinite loop so that function do not terminate and thread do not end.
    while True: 
        
         
        #Receiving from client
        data = conn.recv(1024)
        print data
        
        reply = 'OK...' + data + str(addr)
        if not data:
            break
     
        conn.sendall(reply)
     
    #came out of loop
    conn.close()
 
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])
     
    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))
 
s.close()

I put this together and am having some doubts to the number of connections I can make. I also doubt if I can brute force the server to serve up connections and then hang.

Please take a look and tell me what I can do better.

'''
    Simple socket server using threads
'''

import socket
import sys
from thread import *


HOST = '192.168.0.102'   # Symbolic name meaning all available interfaces
PORT = 8887 # Arbitrary non-privileged port
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print 'Socket created'
 
#Bind socket to local host and port

try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
 
#Start listening on socket
s.listen(10)

print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(conn):
    
     
    #infinite loop so that function do not terminate and thread do not end.
    while True: 
        
         
        #Receiving from client
        data = conn.recv(1024)
        print data
        
        reply = 'OK...' + data + str(addr)
        if not data:
            break
     
        conn.sendall(reply)
     
    #came out of loop
    conn.close()
 
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])
     
    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))
 
s.close()
Source Link
kg_root
  • 81
  • 1
  • 3

Python Socket Receiver

Hello can someone help me with these code. I put it together and having some doubts to the number of connections I can make and also if I can brute force the server to serve up connections and then hang.

Please take a look and tell me what I can do better, suggestions etc.

Thanks!

'''
    Simple socket server using threads
'''

import socket
import sys
from thread import *


HOST = '192.168.0.102'   # Symbolic name meaning all available interfaces
PORT = 8887 # Arbitrary non-privileged port
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print 'Socket created'
 
#Bind socket to local host and port

try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
 
#Start listening on socket
s.listen(10)

print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(conn):
    
     
    #infinite loop so that function do not terminate and thread do not end.
    while True: 
        
         
        #Receiving from client
        data = conn.recv(1024)
        print data
        
        reply = 'OK...' + data + str(addr)
        if not data:
            break
     
        conn.sendall(reply)
     
    #came out of loop
    conn.close()
 
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])
     
    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))
 
s.close()