While and If loop in one line
I have created a client server app, where when i am in need to update the Client code, i am pushing an update using a string match and whenever that string matches the infinitely running client code process that.
The update is nothing but a string that contains python code in it, let's say this:
while True:
if data == "exit":
connection_status = 'Exit Request Received, Exiting'
MessageBox(None, connection_status, "Connection Status", 0x40000 | MB_OK | ICON_STOP)
break
And in my client main code, i have something like this:
if 'update' in data:
new_source = data.split('###_CODEBLOCK_###')
exec(new_source[1])
From the server, i am sending something like this:
Enter you Message : update code is here ###_CODEBLOCK_###while True : if data == "mad": connection_status = "Exit Request Received, Exiting" ; MessageBox(None, connection_status, "Connection Status", 0x40000 | MB_OK | ICON_STOP) ; break
Problem :
CLI doesn't like new line, therefore i have to use one-liner python code to achieve this
Because, it's a one-liner and while and if loop are compound statements, i can't use them the way i put in, so i need to know how can i achieve it in one-line code
Is this really a good hack to send updates? This is bonus question for me, if you don't answer it, that's fine
python python-2.7
add a comment |
I have created a client server app, where when i am in need to update the Client code, i am pushing an update using a string match and whenever that string matches the infinitely running client code process that.
The update is nothing but a string that contains python code in it, let's say this:
while True:
if data == "exit":
connection_status = 'Exit Request Received, Exiting'
MessageBox(None, connection_status, "Connection Status", 0x40000 | MB_OK | ICON_STOP)
break
And in my client main code, i have something like this:
if 'update' in data:
new_source = data.split('###_CODEBLOCK_###')
exec(new_source[1])
From the server, i am sending something like this:
Enter you Message : update code is here ###_CODEBLOCK_###while True : if data == "mad": connection_status = "Exit Request Received, Exiting" ; MessageBox(None, connection_status, "Connection Status", 0x40000 | MB_OK | ICON_STOP) ; break
Problem :
CLI doesn't like new line, therefore i have to use one-liner python code to achieve this
Because, it's a one-liner and while and if loop are compound statements, i can't use them the way i put in, so i need to know how can i achieve it in one-line code
Is this really a good hack to send updates? This is bonus question for me, if you don't answer it, that's fine
python python-2.7
You have basically reinvented a trojan.
– Andrew Cheong
Nov 22 '18 at 7:25
You can create a function with your code and call it when you need it.
– bhawesh chandola
Nov 22 '18 at 7:31
add a comment |
I have created a client server app, where when i am in need to update the Client code, i am pushing an update using a string match and whenever that string matches the infinitely running client code process that.
The update is nothing but a string that contains python code in it, let's say this:
while True:
if data == "exit":
connection_status = 'Exit Request Received, Exiting'
MessageBox(None, connection_status, "Connection Status", 0x40000 | MB_OK | ICON_STOP)
break
And in my client main code, i have something like this:
if 'update' in data:
new_source = data.split('###_CODEBLOCK_###')
exec(new_source[1])
From the server, i am sending something like this:
Enter you Message : update code is here ###_CODEBLOCK_###while True : if data == "mad": connection_status = "Exit Request Received, Exiting" ; MessageBox(None, connection_status, "Connection Status", 0x40000 | MB_OK | ICON_STOP) ; break
Problem :
CLI doesn't like new line, therefore i have to use one-liner python code to achieve this
Because, it's a one-liner and while and if loop are compound statements, i can't use them the way i put in, so i need to know how can i achieve it in one-line code
Is this really a good hack to send updates? This is bonus question for me, if you don't answer it, that's fine
python python-2.7
I have created a client server app, where when i am in need to update the Client code, i am pushing an update using a string match and whenever that string matches the infinitely running client code process that.
The update is nothing but a string that contains python code in it, let's say this:
while True:
if data == "exit":
connection_status = 'Exit Request Received, Exiting'
MessageBox(None, connection_status, "Connection Status", 0x40000 | MB_OK | ICON_STOP)
break
And in my client main code, i have something like this:
if 'update' in data:
new_source = data.split('###_CODEBLOCK_###')
exec(new_source[1])
From the server, i am sending something like this:
Enter you Message : update code is here ###_CODEBLOCK_###while True : if data == "mad": connection_status = "Exit Request Received, Exiting" ; MessageBox(None, connection_status, "Connection Status", 0x40000 | MB_OK | ICON_STOP) ; break
Problem :
CLI doesn't like new line, therefore i have to use one-liner python code to achieve this
Because, it's a one-liner and while and if loop are compound statements, i can't use them the way i put in, so i need to know how can i achieve it in one-line code
Is this really a good hack to send updates? This is bonus question for me, if you don't answer it, that's fine
python python-2.7
python python-2.7
edited Nov 22 '18 at 7:23


eyllanesc
79.6k103258
79.6k103258
asked Nov 22 '18 at 7:18
PetPanPetPan
1239
1239
You have basically reinvented a trojan.
– Andrew Cheong
Nov 22 '18 at 7:25
You can create a function with your code and call it when you need it.
– bhawesh chandola
Nov 22 '18 at 7:31
add a comment |
You have basically reinvented a trojan.
– Andrew Cheong
Nov 22 '18 at 7:25
You can create a function with your code and call it when you need it.
– bhawesh chandola
Nov 22 '18 at 7:31
You have basically reinvented a trojan.
– Andrew Cheong
Nov 22 '18 at 7:25
You have basically reinvented a trojan.
– Andrew Cheong
Nov 22 '18 at 7:25
You can create a function with your code and call it when you need it.
– bhawesh chandola
Nov 22 '18 at 7:31
You can create a function with your code and call it when you need it.
– bhawesh chandola
Nov 22 '18 at 7:31
add a comment |
1 Answer
1
active
oldest
votes
You can use exec() function in Python and convert that multiline code into a single line string to be used as exec()
argument. Make sure you use n
to indicate newline and still keep the proper spacing to indicate indentation.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425699%2fwhile-and-if-loop-in-one-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use exec() function in Python and convert that multiline code into a single line string to be used as exec()
argument. Make sure you use n
to indicate newline and still keep the proper spacing to indicate indentation.
add a comment |
You can use exec() function in Python and convert that multiline code into a single line string to be used as exec()
argument. Make sure you use n
to indicate newline and still keep the proper spacing to indicate indentation.
add a comment |
You can use exec() function in Python and convert that multiline code into a single line string to be used as exec()
argument. Make sure you use n
to indicate newline and still keep the proper spacing to indicate indentation.
You can use exec() function in Python and convert that multiline code into a single line string to be used as exec()
argument. Make sure you use n
to indicate newline and still keep the proper spacing to indicate indentation.
answered Nov 22 '18 at 7:28
AndreasAndreas
1,99931218
1,99931218
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425699%2fwhile-and-if-loop-in-one-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You have basically reinvented a trojan.
– Andrew Cheong
Nov 22 '18 at 7:25
You can create a function with your code and call it when you need it.
– bhawesh chandola
Nov 22 '18 at 7:31