python - 一个文件中两个子进程的套接字通信

标签 python sockets operating-system multiprocessing

我正在两个文件(客户端和服务器)之间进行信息交换,该信息交换效果非常好,但是我在将代码实现到具有两个子进程的一个文件中时遇到了麻烦。截至目前,有一个父级生成了一个与另一个客户端通信的子级。

我尝试将所有套接字绑定(bind)放入子进程中,但无济于事。它只打印最上面的两行。 我应该寻找什么?

感谢大家的帮助

server.py(有效)

#!/usr/bin/python           # This is server.py file
import socket               # Import socket module
import random
import os

g=101 # publicly known
p=103 # publicly known

x= random.randint(1,100) # Alice's random number
y= random.randint(1,100) # Bob's random number

aliceSends = (p**x)%g
bobComputes = (aliceSends**y)%g
bobSends = (p**y)%g

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection up to 5.

print 'Qsocket: Using sockets for IPC'
print 'Diffie-Hellman Parameters p=103 and g=101'
print 'Parent: pid= ',os.getpid(),'\n'

while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Alice Accepted connection from Bob\n'

   if os.fork() == 0:
      #sends first message
      print 'Alice: pid= ',os.getpid(), ', my Parent pid= ',os.getppid()
      print 'Alice Random Secret: ' + str(x)
      print 'Alice Public T: ' + str(aliceSends)
      print 'Alice send to Bob its Public T: ',aliceSends,'\n'
      c.send(str(aliceSends))

      #receieves message
      bobSends = int(c.recv(1024))

      print '<< Alice Got Bob Public T: ',str(bobSends)
      aliceComputes = (bobSends**x)%g
      print 'Alice-to-Bob Shared Secret: << (' + str(aliceComputes) + ') >>'

   c.close()                # Close the connection

print 'Parent: Alice and Bob exited'

client.py(有效)

#!/usr/bin/python           # This is client.py file
import socket               # Import socket module
import random
import os

g=101 # publicly known
p=103 # publicly known

y= random.randint(1,100) # Bob's random number

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.

s.connect((host, port))
#receieves first message
print 'Bob: Connected to Alice\n'
print 'Bob: pid= ',os.getpid()
print 'Bob Random Secret: ' + str(y)
bobSends = (p**y)%g
print 'Bob Public T: ' + str(bobSends),'\n'

aliceSends = int(s.recv(1024))
bobComputes = str((aliceSends**y)%g)

print '>> Bob Got Alice Public T: ',str(aliceSends)
print 'Bob-to-Alice Shared Secret: << (' + str(bobComputes) +') >>'
print 'Bob send to Alice its Public T: ',bobSends


#sends message back
s.send(str(bobSends))
s.close                     # Close the socket when done

这是上面的压缩代码,与我想要的类似,但它卡在服务器调用上并且永远不会到达客户端: cliserv.py(已修复)

#!/usr/bin/python           # This is server.py file
import socket               # Import socket module
import random
import os

g=101 # publicly known
p=103 # publicly known

x= random.randint(1,100) # Alice's random number
y= random.randint(1,100) # Bob's random number

aliceSends = (p**x)%g
bobComputes = (aliceSends**y)%g
bobSends = (p**y)%g

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = random.randint(0,65500)                # Reserve a port for your service.

print 'Qsocket: Using sockets for IPC'
print 'Diffie-Hellman Parameters p=103 and g=101'
print 'Parent: pid= ',os.getpid(),'\n'

if os.fork() == 0:
   s = socket.socket()
   s.bind((host, port))        # Bind to the port
   s.listen(5)                 # Now wait for client connection up to 5.
   c, addr = s.accept()     # Establish connection with client.

   print 'Alice Accepted connection from Bob\n'

   #sends first message
   print 'Alice: pid= ',os.getpid(), ', my Parent pid= ',os.getppid()
   print 'Alice Random Secret: ' + str(x)
   print 'Alice Public T: ' + str(aliceSends)
   print 'Alice send to Bob its Public T: ',aliceSends,'\n'
   c.send(str(aliceSends))

   #receieves message 'thanks for thanking'
   bobSends = int(c.recv(1024))

   print '<< Alice Got Bob Public T: ',str(bobSends)
   aliceComputes = (bobSends**x)%g
   print 'Alice-to-Bob Shared Secret: << (' + str(aliceComputes) + ') >>'
   c.close()                # Close the connection
elif os.fork() ==0:
   g=101 # publicly known
   p=103 # publicly known

   s = socket.socket()
   s.connect((host, port))
   #receieves first message
   print 'Bob: Connected to Alice\n'
   print 'Bob: pid= ',os.getpid()
   print 'Bob Random Secret: ' + str(y)
   bobSends = (p**y)%g
   print 'Bob Public T: ' + str(bobSends),'\n'

   aliceSends = int(s.recv(1024))
   bobComputes = str((aliceSends**y)%g)

   print '>> Bob Got Alice Public T: ',str(aliceSends)
   print 'Bob-to-Alice Shared Secret: << (' + str(bobComputes) +') >>'
   print 'Bob send to Alice its Public T: ',bobSends


   #sends message back
   s.send(str(bobSends))
   s.close                     # Close the socket when done
else:
   #parent
   os.wait()
   # one child has exited
   os.wait()
   # both children have exited
   print 'Parent: Alice and Bob exited'

输出应如下所示:

Qsocket: Using sockets for IPC
Diffie-Hellman Parameters p=103 and g=101
Parent: pid=  5462
Alice: pid=  5463  my Parent pid=  5462 

Alice Random Secret: 64
Alice Public T:  79
Alice send to Bob its Public T:  79 

Bob: pid=  5464  my Parent pid=  5462 

Bob Random Secret: 85
Bob Public T: 62

>> Bob Got Alice Public T:  79
Bob-to-Alice Shared Secret: << (36) >>
Bob send to Alice its Public T:  62 

<< Alice Got Bob Public T:  62
Alice-to-Bob Shared Secret: <<(36)>>

Parent: Alice Exited
Parent: Bob Exited

编辑:更新了代码,以便所有三个版本都可以工作。

最佳答案

重新发布我的回答 http://www.reddit.com/r/learnprogramming/comments/1ywkut/python_socket_communication_by_two_children/ .

修复代码的步骤是。

  1. 摆脱 while 循环。这是没有必要的,并且在 while 循环内使用 fork 可能会导致 fork 炸弹。
  2. 服务器和客户端进程应使用单独的套接字和网络逻辑。但是,它们应该使用相同的端口号。
  3. 父进程应在最后调用 os.wait 两次,以确保两个子进程均已退出。

关于python - 一个文件中两个子进程的套接字通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22019671/

相关文章:

c - recvfrom() 中的 addrlen 字段是做什么用的?

java - 根据使用的操作系统在 Swing 中加载图像图标

python - 为什么 LD_PRELOAD 不能与 Python 一起使用?

python - Python 中的 Trie(前缀树)

c - 如果启用环回,为什么发件人没有收到其多播 UDP 数据包?

c - 在c中为轮询添加回调函数

c++ - 如何在自制操作系统中获取键盘输入?

c - 堆栈内存有限制吗?

Python 多处理和 tkinter - 如何连接此进程(GUI 和衍生进程)?

python - 对象中的一个参数下有多个值