Keep socket open until client terminates












0















I am passing a user input license key (client) to a validation (server) that maps the user input to the valid licenses in my licensing API. I need the server connection to stay open until the client sends the correct license - maybe close the connection after 3 attempts. When I open the server socket it closes upon receiving the first input from the client - whether it is a good request or bad. How do I close the connection only on successful validation?



I tried wrapping the validation check in a loop but this becomes recursive as we need the user to initiate the validation check when they click submit from tkinter button on client.



import requests
from xml.etree import ElementTree as ET
import socket

key = '***'
secret = '***'
request_type = {'Content-type': 'text/xml', 'Accept': 'text/xml'}
productURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1/products'
orderURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1_3/orders'

order_id =
lic_key =

def Main():
data = ""
host = "127.0.0.1"
port = 5000

mySocket = socket.socket()
mySocket.bind((host,port))

mySocket.listen(5)
conn, addr = mySocket.accept()
print ("Connection from: " + str(addr))
user_key = conn.recv(1024).decode()

order_response = requests.get(orderURL, headers=request_type)

order_root = ET.fromstring(order_response.content)

for child in order_root.iter('*'):
if child.tag == 'id':
order_id.append(child.text)

i = 0
while i < len(order_id):
licenseURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1/orders/'+order_id[i]+'/licenses'
lic_response = requests.get(licenseURL, headers=request_type)
lic_root = ET.fromstring(lic_response.content)

for child in lic_root.iter('*'):
if child.tag == 'key':
lic_key.append(child.text)
i += 1

print("validating...........")
if data != "SUCCESS":
if user_key in lic_key:
message = "Success"
else:
message = "Failure"
data = str(message).upper()
print ("sending: " + str(data))
conn.send(data.encode())

if __name__ == '__main__':
Main()









share|improve this question























  • Read how to create a Minimal, Complete, and Verifiable example. Your server in this example never closes the connection directly, but only reads one blob and sends one blob. When Main() returns, mySocket is out of scope closing the connection. Note that TCP is a streaming protocol, so recv(1024) can receive any amount of data from 0 (client closed connection) to 1024 bytes of data. You'll need to define a protocol and ensure you read until you have a complete message, and send complete messages. After accepting the connection, loop on receiving messages and sending good/bad responses until you have a good message.

    – Mark Tolonen
    Dec 28 '18 at 23:44













  • This answer I recently gave has an example of a message protocol.

    – Mark Tolonen
    Dec 28 '18 at 23:49
















0















I am passing a user input license key (client) to a validation (server) that maps the user input to the valid licenses in my licensing API. I need the server connection to stay open until the client sends the correct license - maybe close the connection after 3 attempts. When I open the server socket it closes upon receiving the first input from the client - whether it is a good request or bad. How do I close the connection only on successful validation?



I tried wrapping the validation check in a loop but this becomes recursive as we need the user to initiate the validation check when they click submit from tkinter button on client.



import requests
from xml.etree import ElementTree as ET
import socket

key = '***'
secret = '***'
request_type = {'Content-type': 'text/xml', 'Accept': 'text/xml'}
productURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1/products'
orderURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1_3/orders'

order_id =
lic_key =

def Main():
data = ""
host = "127.0.0.1"
port = 5000

mySocket = socket.socket()
mySocket.bind((host,port))

mySocket.listen(5)
conn, addr = mySocket.accept()
print ("Connection from: " + str(addr))
user_key = conn.recv(1024).decode()

order_response = requests.get(orderURL, headers=request_type)

order_root = ET.fromstring(order_response.content)

for child in order_root.iter('*'):
if child.tag == 'id':
order_id.append(child.text)

i = 0
while i < len(order_id):
licenseURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1/orders/'+order_id[i]+'/licenses'
lic_response = requests.get(licenseURL, headers=request_type)
lic_root = ET.fromstring(lic_response.content)

for child in lic_root.iter('*'):
if child.tag == 'key':
lic_key.append(child.text)
i += 1

print("validating...........")
if data != "SUCCESS":
if user_key in lic_key:
message = "Success"
else:
message = "Failure"
data = str(message).upper()
print ("sending: " + str(data))
conn.send(data.encode())

if __name__ == '__main__':
Main()









share|improve this question























  • Read how to create a Minimal, Complete, and Verifiable example. Your server in this example never closes the connection directly, but only reads one blob and sends one blob. When Main() returns, mySocket is out of scope closing the connection. Note that TCP is a streaming protocol, so recv(1024) can receive any amount of data from 0 (client closed connection) to 1024 bytes of data. You'll need to define a protocol and ensure you read until you have a complete message, and send complete messages. After accepting the connection, loop on receiving messages and sending good/bad responses until you have a good message.

    – Mark Tolonen
    Dec 28 '18 at 23:44













  • This answer I recently gave has an example of a message protocol.

    – Mark Tolonen
    Dec 28 '18 at 23:49














0












0








0








I am passing a user input license key (client) to a validation (server) that maps the user input to the valid licenses in my licensing API. I need the server connection to stay open until the client sends the correct license - maybe close the connection after 3 attempts. When I open the server socket it closes upon receiving the first input from the client - whether it is a good request or bad. How do I close the connection only on successful validation?



I tried wrapping the validation check in a loop but this becomes recursive as we need the user to initiate the validation check when they click submit from tkinter button on client.



import requests
from xml.etree import ElementTree as ET
import socket

key = '***'
secret = '***'
request_type = {'Content-type': 'text/xml', 'Accept': 'text/xml'}
productURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1/products'
orderURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1_3/orders'

order_id =
lic_key =

def Main():
data = ""
host = "127.0.0.1"
port = 5000

mySocket = socket.socket()
mySocket.bind((host,port))

mySocket.listen(5)
conn, addr = mySocket.accept()
print ("Connection from: " + str(addr))
user_key = conn.recv(1024).decode()

order_response = requests.get(orderURL, headers=request_type)

order_root = ET.fromstring(order_response.content)

for child in order_root.iter('*'):
if child.tag == 'id':
order_id.append(child.text)

i = 0
while i < len(order_id):
licenseURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1/orders/'+order_id[i]+'/licenses'
lic_response = requests.get(licenseURL, headers=request_type)
lic_root = ET.fromstring(lic_response.content)

for child in lic_root.iter('*'):
if child.tag == 'key':
lic_key.append(child.text)
i += 1

print("validating...........")
if data != "SUCCESS":
if user_key in lic_key:
message = "Success"
else:
message = "Failure"
data = str(message).upper()
print ("sending: " + str(data))
conn.send(data.encode())

if __name__ == '__main__':
Main()









share|improve this question














I am passing a user input license key (client) to a validation (server) that maps the user input to the valid licenses in my licensing API. I need the server connection to stay open until the client sends the correct license - maybe close the connection after 3 attempts. When I open the server socket it closes upon receiving the first input from the client - whether it is a good request or bad. How do I close the connection only on successful validation?



I tried wrapping the validation check in a loop but this becomes recursive as we need the user to initiate the validation check when they click submit from tkinter button on client.



import requests
from xml.etree import ElementTree as ET
import socket

key = '***'
secret = '***'
request_type = {'Content-type': 'text/xml', 'Accept': 'text/xml'}
productURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1/products'
orderURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1_3/orders'

order_id =
lic_key =

def Main():
data = ""
host = "127.0.0.1"
port = 5000

mySocket = socket.socket()
mySocket.bind((host,port))

mySocket.listen(5)
conn, addr = mySocket.accept()
print ("Connection from: " + str(addr))
user_key = conn.recv(1024).decode()

order_response = requests.get(orderURL, headers=request_type)

order_root = ET.fromstring(order_response.content)

for child in order_root.iter('*'):
if child.tag == 'id':
order_id.append(child.text)

i = 0
while i < len(order_id):
licenseURL = 'https://'+key+':'+secret+'@www.sendowl.com/api/v1/orders/'+order_id[i]+'/licenses'
lic_response = requests.get(licenseURL, headers=request_type)
lic_root = ET.fromstring(lic_response.content)

for child in lic_root.iter('*'):
if child.tag == 'key':
lic_key.append(child.text)
i += 1

print("validating...........")
if data != "SUCCESS":
if user_key in lic_key:
message = "Success"
else:
message = "Failure"
data = str(message).upper()
print ("sending: " + str(data))
conn.send(data.encode())

if __name__ == '__main__':
Main()






python sockets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 19:18









Chris BeckChris Beck

1




1













  • Read how to create a Minimal, Complete, and Verifiable example. Your server in this example never closes the connection directly, but only reads one blob and sends one blob. When Main() returns, mySocket is out of scope closing the connection. Note that TCP is a streaming protocol, so recv(1024) can receive any amount of data from 0 (client closed connection) to 1024 bytes of data. You'll need to define a protocol and ensure you read until you have a complete message, and send complete messages. After accepting the connection, loop on receiving messages and sending good/bad responses until you have a good message.

    – Mark Tolonen
    Dec 28 '18 at 23:44













  • This answer I recently gave has an example of a message protocol.

    – Mark Tolonen
    Dec 28 '18 at 23:49



















  • Read how to create a Minimal, Complete, and Verifiable example. Your server in this example never closes the connection directly, but only reads one blob and sends one blob. When Main() returns, mySocket is out of scope closing the connection. Note that TCP is a streaming protocol, so recv(1024) can receive any amount of data from 0 (client closed connection) to 1024 bytes of data. You'll need to define a protocol and ensure you read until you have a complete message, and send complete messages. After accepting the connection, loop on receiving messages and sending good/bad responses until you have a good message.

    – Mark Tolonen
    Dec 28 '18 at 23:44













  • This answer I recently gave has an example of a message protocol.

    – Mark Tolonen
    Dec 28 '18 at 23:49

















Read how to create a Minimal, Complete, and Verifiable example. Your server in this example never closes the connection directly, but only reads one blob and sends one blob. When Main() returns, mySocket is out of scope closing the connection. Note that TCP is a streaming protocol, so recv(1024) can receive any amount of data from 0 (client closed connection) to 1024 bytes of data. You'll need to define a protocol and ensure you read until you have a complete message, and send complete messages. After accepting the connection, loop on receiving messages and sending good/bad responses until you have a good message.

– Mark Tolonen
Dec 28 '18 at 23:44







Read how to create a Minimal, Complete, and Verifiable example. Your server in this example never closes the connection directly, but only reads one blob and sends one blob. When Main() returns, mySocket is out of scope closing the connection. Note that TCP is a streaming protocol, so recv(1024) can receive any amount of data from 0 (client closed connection) to 1024 bytes of data. You'll need to define a protocol and ensure you read until you have a complete message, and send complete messages. After accepting the connection, loop on receiving messages and sending good/bad responses until you have a good message.

– Mark Tolonen
Dec 28 '18 at 23:44















This answer I recently gave has an example of a message protocol.

– Mark Tolonen
Dec 28 '18 at 23:49





This answer I recently gave has an example of a message protocol.

– Mark Tolonen
Dec 28 '18 at 23:49












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963317%2fkeep-socket-open-until-client-terminates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963317%2fkeep-socket-open-until-client-terminates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'