Python - Multiprocessing changes socket's value
<p>I'm trying to program an MD5 decryptor which uses LAN to decrypt, and using all the available CPU on each PC.
The client code:</p>

<pre><code>import socket
import multiprocessing as mp
import select
import re
import hashlib
import sys

def decryptCall(list,text):
    rangeNums = int(list[1]) - int(list[0]) + 1
    for i in range(len(list)):
        print my_socket
        print "ok"
        decrypt(list[i], int(list[i]) + rangeNums, text)
        #p = mp.Process(target=decrypt,args=(list[i], int(list[i]) + rangeNums, text))
        #p.start()
        #processes.append(p)

def end():
    for p in processes:
        p.join()
    print my_socket

    print "goodbye"
    sys.exit()
def decrypt(low,high,text):
    counter = int(low)
    high = int(high)
    while counter &lt;= high:
        m = hashlib.new('md5')
        m.update(str(counter))
        if str(m.hexdigest()).lower() == str(text.lower()):
            print "final: " + str(counter)
            my_socket.send(str(counter))
            end()
        counter+=1

if __name__ == "__main__":
    processes = []
    dataList = []
    q = mp.Queue()
    ADDR = ('127.0.0.1', 1337)
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    my_socket.connect(ADDR)
    my_socket.send(str(mp.cpu_count()))

    while True:
        rlist, wlist, xlist = select.select([my_socket], [my_socket], [])
        for current_socket in rlist:
            data = current_socket.recv(1024)
            if not re.search('[a-zA-Z]', data):
                dataList.append(data)
            else:
                md5hash = data
                print md5hash
                print dataList
                print my_socket
                decryptCall(dataList,md5hash)
</code></pre>

<p>The server code:</p>

<pre><code>import time
import hashlib
import socket
import select
import msvcrt
import sys

open_client_sockets=[]

def decrypt(cpus2,text,):
    seperated = []
    place = 0
    low = 1000000
    high = 9999999
    numbers = high-low
    numbers /= cpus2
    for g in range(cpus2):
        seperated.append(low + numbers*g)
        print open_client_sockets[0]
    for g in range(len(open_client_sockets)):
        print str(g) + ": " + str(cpus.get(open_client_sockets[g]))
        for h in range(int(cpus.get(open_client_sockets[g]))):

            time.sleep(0.01)
            open_client_sockets[g].send(str(seperated[place]))
            place+=1
        open_client_sockets[g].send(text)
    test()

def test():
    print open_client_sockets[0].recv(10000)

if __name__ == "__main__":
    md5hash2 = "fcea920f7412b5da7be0cf42b8c93759"
    messages_to_send=[]
    input = ''
    addresses = {}
    cpus = {}
    cpus_count=0
    server_socket = socket.socket()
    server_socket.bind(('0.0.0.0', 1337))
    server_socket.listen(5)

    while True:
        rlist, wlist, xlist = select.select( [server_socket] + open_client_sockets, open_client_sockets,[])
        for current_socket in rlist:
            if current_socket is server_socket:
                (new_socket, address) = server_socket.accept()
                addresses[new_socket] = address
                open_client_sockets.append(new_socket)
                #print addresses
            else:
                data = current_socket.recv(1024)
                cpus_count += int(data)
                cpus[current_socket] = data
        if msvcrt.kbhit():
            key = msvcrt.getch()
            sys.stdout.write(key)
            if key == '\r' and input != "":
                print "\nok"
                print cpus_count
                decrypt(cpus_count,md5hash2)
            input += key
</code></pre>

<p>The problem is, when I try to use multiprocessing:</p>

<pre><code> decrypt(list[i], int(list[i]) + rangeNums, text)
            p = mp.Process(target=decrypt,args=(list[i], int(list[i]) + rangeNums, text))
            p.start()
            processes.append(p)
</code></pre>

<p>The data isn't sent, because the socket's value changes. Example:</p>

<pre><code>&lt;socket._socketobject object at 0x029CAB58&gt; -&gt; &lt;socket._socketobject object at 0x02941AE8&gt;.
</code></pre>

<p>While trying to figure it out, I noticed something weird. 
Every time the program creates a new process, the socket's value changes.
When I use the normal call to function without multiprocessing, the socket stays as it is and the data is sent.</p>

<p>I don't have a fully formed answer( I'd leave a comment if my reputation was higher).  I believe what you are seeing is a result of trying to use a socket in a fork()d child that was established in a parent process (it's still open in the parent).  In this case you're probably getting a copy of the parent.  </p>

<p>Some solutions:
You could establish a new socket in the child.<br>
You could close the connection in the parent right after fork (or process start in this case).</p>

<p>For more reading lookup fork and socket, or multiprocessing and socket.</p>

python sockets encryption multiprocessing md5