2016-08-05 88 views
2

我尝试使用套接字来发送JSON数据发送JSON数据,但得到这个错误如何使用SSL

b'HTTP/0.9 413请求实体过大\ r \ n服务器: inets/5.10 .2 \ r \ n日期:2016年8月5日星期五18:19:38 GMT \ r \ n内容类型: text/html \ r \ n内容长度:202 \ r \ n \ r \ n'

我的代码:

def single_contract(amount, service_key): 

    current_date = datetime.now() 
    fee = 0 
    if amount > 10000.0: 
     fee = amount * 0.01 

    if os.path.exists('./Files/Point.txt'): 
     with open('./Files/Point.txt') as opened_file: 
      point_id = opened_file.read() 

      json_data = dict() 
      json_data["single_contract"] = dict() 
      json_data["single_contract"]["point_id"] = point_id.strip() 
      json_data["single_contract"]["datetime"] = datetime.strftime(current_date, '%Y-%m-%d %H:%M:%S') 
      json_data["single_contract"]["external_transaction_id"] = randint(999999999, 9999999999) 
      json_data["single_contract"]["service_id"] = 1001351861392575516 
      json_data["single_contract"]["amount"] = amount 
      json_data["single_contract"]["service_key"] = service_key 
      json_data["single_contract"]["fee"] = round(fee, 2) 
      json_data["single_contract"]["params"] = None 

     with open('./Files/sending_file.json', mode='w', encoding='utf-8') as json_file: 
      json.dump(json_data, json_file) 

     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
     sslcontext = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH) 
     sslcontext.load_verify_locations('./Files/ca-cert.pem') 
     sslcontext.load_cert_chain(certfile='./Files/com-cert.pem', 
            keyfile='./Files/com-key.pem') 
     host = '***.***.***.***' 
     port = **** 
     ssl_socket = sslcontext.wrap_socket(sock, server_hostname=host) 
     ssl_socket.connect((host, port)) 

     with open('./Files/sending_file.json', mode='rb') as f: 
      ssl_socket.sendfile(f) 

     with open('./Files/answer.txt', mode='w', encoding='utf-8') as reply: 
      reply.write(str(ssl_socket.recv())) 

     ssl_socket.close() 
     print('End of connection') 

    else: 
     print('No such file or directory.') 

什么是I D在这里错了吗?谢谢

+0

代码中的缩进看起来搞砸了。 – jmdeamer

回答

0

您发送的数据太多。您需要减少发送内容的大小,或者如果您控制了服务器,请调整其设置以允许它接收更大的Https请求。

http://cnedelcu.blogspot.com/2013/09/nginx-error-413-request-entity-too-large.html

+0

我的数据文件大小为215个字节。 –

+0

看起来并不多,但是您收到的是服务器生成的错误消息,说您发送的内容太大。所以即使它看起来很小,显然它在服务器的最大值之上。这是在服务器上配置的东西。 – bravosierra99

+0

谢谢。如果他们没问题,我会试着询问服务器的设置。 –