2016-08-05 92 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

謝謝。如果他們沒問題,我會試着詢問服務器的設置。 –