如何使用python套接字从服务器向客户端发送响应?

时间:2017-05-15 09:35:12

标签: python sockets networking

我想使用python套接字在两个系统之间进行通信,其中一台机器是本地系统(客户机),另一台机器是公共系统(服务器),

我写的代码是:

client.py

import socket
import commands
import sys
import time


s = socket.socket()
state = sys.argv[1]

pname = sys.argv[2]
user = commands.getoutput("whoami")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('132.65.45.156', 5001))


info = commands.getoutput("echo '%s','%s','%s','%s'"%(state,ip,user,pname))
while True:
   data2 = info
   s.sendall(data2)
break

server.py

import socket
import time
import string
import sys
import base64


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 5001))
sock.listen(5)

while True:
newSocket, address = sock.accept()
print "Connected from ", address
filename = newSocket.recv(5001) 
while True:     
data = newSocket.recv(5001)     
if not data: break
print filename, "Received\n"
sock.close()

收到客户端到服务器的响应后,如果服务器想要向客户端发送响应,该怎么办?

0 个答案:

没有答案