Python 客户端未收到数据
我对 Python 很陌生,有一个基本问题,网络套接字连接的客户端可以接收数据吗?在我的问题中,客户端是发起连接的人,这可能是显而易见的,但我想澄清一下。我问是因为我有另一个服务器和客户端(都是Python),允许服务器从客户端接收文件。它工作完美,但我无法获得客户端接收文件的示例。 Python 不断告诉我管道已损坏,我怀疑是因为在客户端我使用了 data = mysocket.recv(1024)
行。我怀疑客户端看不到任何数据流动,因此关闭了与服务器的连接。服务器将其视为破损的管道。服务器和客户端如下。
服务器:
#libraries to import
import socket
import os
import sys
import M2Crypto as m2c
#information about the server the size of the message being transferred
server_address = '10.1.1.2'
key_port = 8888
max_transfer_block = 1024
FILE = open("sPub.pem","r")
#socket for public key transfer
keysocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
keysocket.bind((server_address, key_port))
keysocket.listen(1)
while 1:
conn, client_addr = keysocket.accept()
print 'connected by', client_addr
#sends data to the client
data = FILE.read()
keysocket.sendall(data)
keysocket.close()
客户端:
# the specified libraries
import socket
import M2Crypto as m2c
#file to be transferred
FILE = open("test.txt", "r")
#address of the server
server_addr = '10.1.1.2'
#port the server is listening on
dest_port = 8888
data = FILE.read()
#Creates a socket for the transfer
mysocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
mysocket.connect( (server_addr, dest_port) )
data = mysocket.recv(1024)
print data
#creates a new file to store the msg
name = "serverPubKey.pem"
#opens the new file and writes the msg to it
FILE = open (name, "w")
FILE.write(data)
#closes the socket.
mysocket.close()
对于此事,我将不胜感激。谢谢!
I am very new to Python and have a basic question, can a client side of a network socket connection make receive data? In my question, the client is the one who initiates the connection, which is probably obvious but I wanted to be clear. I ask because I have another server and client (both python) that allows the server to receive a file from the client. It works perfectly but I cannot get an example where the client receives a file. Python keeps telling me that the pipe has been broken and I suspect its because on the client side I use the line data = mysocket.recv(1024)
. My suspicion is that client doesn't see any data flowing and thus closes the connection to the server. The server sees it as a broken pipe. The server and client are below.
server:
#libraries to import
import socket
import os
import sys
import M2Crypto as m2c
#information about the server the size of the message being transferred
server_address = '10.1.1.2'
key_port = 8888
max_transfer_block = 1024
FILE = open("sPub.pem","r")
#socket for public key transfer
keysocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
keysocket.bind((server_address, key_port))
keysocket.listen(1)
while 1:
conn, client_addr = keysocket.accept()
print 'connected by', client_addr
#sends data to the client
data = FILE.read()
keysocket.sendall(data)
keysocket.close()
Client:
# the specified libraries
import socket
import M2Crypto as m2c
#file to be transferred
FILE = open("test.txt", "r")
#address of the server
server_addr = '10.1.1.2'
#port the server is listening on
dest_port = 8888
data = FILE.read()
#Creates a socket for the transfer
mysocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
mysocket.connect( (server_addr, dest_port) )
data = mysocket.recv(1024)
print data
#creates a new file to store the msg
name = "serverPubKey.pem"
#opens the new file and writes the msg to it
FILE = open (name, "w")
FILE.write(data)
#closes the socket.
mysocket.close()
I would appreciate any help on the matter. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另外,添加之前的评论,有时一个好的测试是重复接收几次。一次性捕获服务器发送的信息的可能性不大。
像这样的东西
Also, adding onto the previous comment, a good test sometimes is to repeat the receive a few times. The chance of the one pass catching the information the server sends is unlikely.
something like this
在这样的应用程序中,有时绕过低级细节并使用 会很有帮助socket.makefile 及其更高级别的 API 代替。
在客户端关闭中,将: 替换
为:
ftplib 的源代码< /a> 展示了如何在生产代码中执行此操作。
In applications like this, it is sometimes helpful to bypass the low level detail and use socket.makefile with its higher-level API instead.
In the client close, replace:
with:
The source code for ftplib shows how to do this in production code.