[英]Connection reset by peer when sending from hardware (GPRS module) but fine when send from PC
tl;dr TL;博士
Connection reset by peer
Connection reset by peer
Local - Setup 本地 - 设置
I have a Quectel M66 , a GPRS module that I'm using to connect to the server ( AWS EC2 ) and transfer some data. 我有一个Quectel M66 ,一个GPRS模块,我用它连接到服务器( AWS EC2 )并传输一些数据。
I also have a python script
I've made to connect and send the same data using a PC. 我还有一个
python script
我用连接并使用PC发送相同的数据。 Here below is the python script 下面是python脚本
import socket
import sys
from io import open
from time import sleep
'''
Python script to send data to remote server
'''
#replace address with the remote server address
HOST, PORT = '127.0.0.1', 3000
if len(sys.argv) < 2:
error = "Error:"
print("{} {}".format(error, "Pass in the file to be send"))
exit(1)
filename = sys.argv[1]
with open(filename, "r", newline="\r") as f:
lines = f.readlines()
data = "".join(lines)
# Create a socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(data, "utf-8"))
# Receive data from the server and shut down
received = r"{}".format(sock.recv(1024))
sock.close()
print("Received: {}".format(received))
Remote - Setup 远程 - 设置
I'm running an EC2-micro
instance that's running a python script that just listens to a port and prints the data it received, also send a response which is hard-coded. 我正在运行一个运行python脚本的
EC2-micro
实例,它只是监听一个端口并打印它收到的数据,同时发送一个硬编码的响应。 Here is the script 这是脚本
#!/usr/bin/env python3
'''
Python code running on EC2-micro
'''
import socket
import errno
from datetime import datetime
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
HOST = '0.0.0.0' # Standard loopback interface address (localhost)
PORT = 3000 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
try:
data = r"{}".format(conn.recv(1024))
print("Received: {}".format(data))
#respond back with $E0A0
conn.sendall(bytes("$E0A0:8B\r$E0FF\r", "utf-8"))
conn.close()
s.close()
break
except socket.error as e:
if e.errno != errno.ECONNRESET:
raise
if e.errno == errno.ECONNRESET:
printf("Connection reset by peer error again")
raise #it is failing here
pass
Quectel GPRS Module Quectel GPRS模块
When I try sending data using the Quectel
module the using the AT Commands
, what I see from the hardware (quectel) side is that the connection has been CLOSED
当我尝试使用
Quectel
模块使用AT Commands
发送数据时,我从硬件(quectel)方面看到的是连接已经CLOSED
17:29:05.652 [Tx] AT+QIOPEN="TCP","127.0.0.1",3000\r
17:29:05.672 [Rx] \r\nOK\r\n\r\nCONNECT OK\r\n
17:29:07.699 [Tx] AT+SEND=1\r
17:29:07.718 [Rx] >
17:29:08.603 [Tx] A
17:29:08.647 [Rx] \r\nSEND OK\r\n
17:29:09.446 [Rx] CLOSED
And the code running on EC2 crashes with: 并且在EC2上运行的代码崩溃:
Connection reset by peer error again
Traceback (most recent call last):
File "./server.py", line 22, in <module>
data = r"{}".format(conn.recv(1024))
ConnectionResetError: [Errno 104] Connection reset by peer
But this doesn't happen when I test using the python script (the first code given above) 但是当我使用python脚本测试时,这不会发生 (上面给出的第一个代码)
Running python script from PC 从PC运行python脚本
$ python client.py data
Received: b'$E0A0:8B\r$E0FF\r'
Some more observations: 更多观察:
load balancing
) it fails with Connection reset by peer
load balancing
),它才会失败,同时Connection reset by peer
CloudWatch
and no spike in CPU usage was seen . CloudWatch
并没有看到CPU使用率的高峰 。 SEND OK
, from the documentation we can be sure that the data has left the module. SEND OK
响应,因此从文档中我们可以确定数据已离开模块。 And TCP promises delivery of data Connection reset by peer
Connection reset by peer
This was a backend issue. 这是一个后端问题。 A port white-listing issue.
端口白名单问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.