0

Basically, i'm trying to run multiprocessing using Python that will utilize telnet to a server in order to send HTTP request. The request is recorded in request.txt full request with header.

In my python code, I've tested the full multiprocessing, it works like magic. However, once placed in the server, the bash commands are not executed properly.

What I'm trying to replicate in Python is the below bash sample:

START=
 (
 cat request.txt 
 sleep 2
 )| telnet 0 999

what I'm doing in my python source code is:

os.system("START=\
 (\
 cat request.txt \
 sleep 2 \
 )| telnet 0 999\
 ")

what I'm getting is:

Trying 0.0.0.0...
Connected to 0.
Escape character is '^]'.
Connection closed by foreign host.

When I run the request manually over bash terminal, it runs perfectly without an error. However, when trying to execute python application, it gives the above behavior.

P/S the reason why I'm not posting the full source code is because my issue is only related to os.system - the bash part of the application.

Thank you

Theg8nj
  • 135
  • 1
  • 16
  • 3
    ... _Why_ are you shelling out to telnet to send this instead of using the socket module? – AKX Jun 08 '20 at 13:53
  • 1
    There are already very good libraries for generating HTTP requests: why aren't you using them? – chepner Jun 08 '20 at 13:55
  • @AKX Python is meant to be a nice glue language, running shell scripts via Python is definitely OK. – alec_djinn Jun 08 '20 at 14:15
  • @alec_djinn, You are mistaken. And I say that as someone who loves Python. It is a horrible language to use in the same way you would use bash. Yes, it is "OK" to do this but it certainly isn't recommended. See the previous two comments which I would upvote more than once if I could. – Kurtis Rader Jun 09 '20 at 03:23
  • @AKX, the server i'm connecting to is using HTTP xml rpc protocol, I will try connecting to it, please share any valuable resources if you have. Thanks – Theg8nj Jun 09 '20 at 11:02
  • @Mr.Curious You're in luck – Python comes with an XML-RPC client built-in. https://docs.python.org/3/library/xmlrpc.client.html#module-xmlrpc.client – AKX Jun 09 '20 at 12:49

1 Answers1

2

I do not know if this is the answer to your problem. But looking at your code, your multiline string evaluates to START=( cat request.txt sleep 2 )| telnet 0 999. So cat request.txt sleep 2 comes as one command. This might be the cause to the problem. You can add a ; after cat request.txt and try.

By the way it is recommended to use triple quotes for multiline strings in python instead of escaping each line with \

Adding the worked solution:

os.system('''START=
     (
     cat vsip.txt;
     sleep 2 
     )| telnet 0 999
     ''')
Theg8nj
  • 135
  • 1
  • 16
Teshan Shanuka J
  • 1,448
  • 2
  • 17
  • 31
  • This is obviously an error in the code so I added this as an answer. But if this fix didn't help with the problem you have, please comment – Teshan Shanuka J Jun 08 '20 at 14:17
  • 1
    thanks, mate. I've updated your answer with the correct format that worked for me for anyone's reference. – Theg8nj Jun 08 '20 at 14:26