http proxy in python, when and how exactly should I close connection?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















For a school project I need to open a proxy server in python. My proxy server works and shows the page on the browser but the problem is that it doesn't close connections after page shown and no more requests sent. The problem happens specifically after Connect Requests that start a proxy tunnel so I don't know when should I close the connection between the client and the server.
When and how should I close the connection between them?



def get_data(sock):
data = b''
data_add = b'test'
try:
while len(data_add) != 0:
# receive data from web server
data_add = sock.recv(4096)
data += data_add
except Exception as e:
print("2:" + str(e) + " ")
return data

def handle_connect_command(client_socket, my_socket):
request = b'test'
try:
send_data(client_socket, b'HTTP/1.1 200 OKrnrn')
while True:
request = get_data(client_socket)
send_data(my_socket, request)
response = get_data(my_socket)
send_data(client_socket, response)
except Exception as e:
print("5:" + str(e))
print("Connection lost")
client_socket.close()
my_socket.close()

def threaded(client_socket):
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
request = b'Test'
try:
while len(request) > 0:
# data received from client
request = get_data(client_socket)
web_server, port, command = analyze_request(request)
print(web_server + ' ' + str(port))
my_socket = connect_to_server(web_server, port)
if command.lower() == "connect":
handle_connect_command(client_socket, my_socket)
break
else:
send_data(my_socket, request)
response = get_data(my_socket)
my_socket.close()
send_data(client_socket, response)
except Exception as e:
print("6:" + str(e))
# connection closed
client_socket.close()
my_socket.close()









share|improve this question

























  • can you show us the code you have so far?

    – mikeg
    Jan 4 at 16:10


















0















For a school project I need to open a proxy server in python. My proxy server works and shows the page on the browser but the problem is that it doesn't close connections after page shown and no more requests sent. The problem happens specifically after Connect Requests that start a proxy tunnel so I don't know when should I close the connection between the client and the server.
When and how should I close the connection between them?



def get_data(sock):
data = b''
data_add = b'test'
try:
while len(data_add) != 0:
# receive data from web server
data_add = sock.recv(4096)
data += data_add
except Exception as e:
print("2:" + str(e) + " ")
return data

def handle_connect_command(client_socket, my_socket):
request = b'test'
try:
send_data(client_socket, b'HTTP/1.1 200 OKrnrn')
while True:
request = get_data(client_socket)
send_data(my_socket, request)
response = get_data(my_socket)
send_data(client_socket, response)
except Exception as e:
print("5:" + str(e))
print("Connection lost")
client_socket.close()
my_socket.close()

def threaded(client_socket):
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
request = b'Test'
try:
while len(request) > 0:
# data received from client
request = get_data(client_socket)
web_server, port, command = analyze_request(request)
print(web_server + ' ' + str(port))
my_socket = connect_to_server(web_server, port)
if command.lower() == "connect":
handle_connect_command(client_socket, my_socket)
break
else:
send_data(my_socket, request)
response = get_data(my_socket)
my_socket.close()
send_data(client_socket, response)
except Exception as e:
print("6:" + str(e))
# connection closed
client_socket.close()
my_socket.close()









share|improve this question

























  • can you show us the code you have so far?

    – mikeg
    Jan 4 at 16:10














0












0








0








For a school project I need to open a proxy server in python. My proxy server works and shows the page on the browser but the problem is that it doesn't close connections after page shown and no more requests sent. The problem happens specifically after Connect Requests that start a proxy tunnel so I don't know when should I close the connection between the client and the server.
When and how should I close the connection between them?



def get_data(sock):
data = b''
data_add = b'test'
try:
while len(data_add) != 0:
# receive data from web server
data_add = sock.recv(4096)
data += data_add
except Exception as e:
print("2:" + str(e) + " ")
return data

def handle_connect_command(client_socket, my_socket):
request = b'test'
try:
send_data(client_socket, b'HTTP/1.1 200 OKrnrn')
while True:
request = get_data(client_socket)
send_data(my_socket, request)
response = get_data(my_socket)
send_data(client_socket, response)
except Exception as e:
print("5:" + str(e))
print("Connection lost")
client_socket.close()
my_socket.close()

def threaded(client_socket):
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
request = b'Test'
try:
while len(request) > 0:
# data received from client
request = get_data(client_socket)
web_server, port, command = analyze_request(request)
print(web_server + ' ' + str(port))
my_socket = connect_to_server(web_server, port)
if command.lower() == "connect":
handle_connect_command(client_socket, my_socket)
break
else:
send_data(my_socket, request)
response = get_data(my_socket)
my_socket.close()
send_data(client_socket, response)
except Exception as e:
print("6:" + str(e))
# connection closed
client_socket.close()
my_socket.close()









share|improve this question
















For a school project I need to open a proxy server in python. My proxy server works and shows the page on the browser but the problem is that it doesn't close connections after page shown and no more requests sent. The problem happens specifically after Connect Requests that start a proxy tunnel so I don't know when should I close the connection between the client and the server.
When and how should I close the connection between them?



def get_data(sock):
data = b''
data_add = b'test'
try:
while len(data_add) != 0:
# receive data from web server
data_add = sock.recv(4096)
data += data_add
except Exception as e:
print("2:" + str(e) + " ")
return data

def handle_connect_command(client_socket, my_socket):
request = b'test'
try:
send_data(client_socket, b'HTTP/1.1 200 OKrnrn')
while True:
request = get_data(client_socket)
send_data(my_socket, request)
response = get_data(my_socket)
send_data(client_socket, response)
except Exception as e:
print("5:" + str(e))
print("Connection lost")
client_socket.close()
my_socket.close()

def threaded(client_socket):
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
request = b'Test'
try:
while len(request) > 0:
# data received from client
request = get_data(client_socket)
web_server, port, command = analyze_request(request)
print(web_server + ' ' + str(port))
my_socket = connect_to_server(web_server, port)
if command.lower() == "connect":
handle_connect_command(client_socket, my_socket)
break
else:
send_data(my_socket, request)
response = get_data(my_socket)
my_socket.close()
send_data(client_socket, response)
except Exception as e:
print("6:" + str(e))
# connection closed
client_socket.close()
my_socket.close()






python http https proxy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 16:34







Dvir

















asked Jan 4 at 16:08









DvirDvir

32




32













  • can you show us the code you have so far?

    – mikeg
    Jan 4 at 16:10



















  • can you show us the code you have so far?

    – mikeg
    Jan 4 at 16:10

















can you show us the code you have so far?

– mikeg
Jan 4 at 16:10





can you show us the code you have so far?

– mikeg
Jan 4 at 16:10












2 Answers
2






active

oldest

votes


















0














Assuming you're using sock/sockets you can simply run:



server.quit()


or



session.close()


if you're using requests.



after creating a server object.



The matter of WHEN to close the connection is something we would need to see your code for.



logically you would close the connection when no more interaction needs to take place






share|improve this answer


























  • is he using sockets, is he using Requests?

    – mikeg
    Jan 4 at 16:15











  • was thinking that myself so updated my response! i dont have permissions to simply make comments yet otherwise i wouldn't have actually submitted an "answer" but here we are -.-

    – Brandon Bailey
    Jan 4 at 16:18











  • i am using sockets

    – Dvir
    Jan 4 at 16:35











  • It looks like you're missing socket.close() in some of your while loops. the while loops should run through a series of commands and then close the connection, instead of only closing the connection if they encounter an error. otherwise the connection will stay open until an error is encountered

    – Brandon Bailey
    Jan 4 at 16:42



















0














If you are using the Requests library (and you should) you can do this.



with requests.Session() as session:
session.get('target_url')


This will close the connection automatically when everything in the with condition completes.






share|improve this answer
























    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%2f54042459%2fhttp-proxy-in-python-when-and-how-exactly-should-i-close-connection%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Assuming you're using sock/sockets you can simply run:



    server.quit()


    or



    session.close()


    if you're using requests.



    after creating a server object.



    The matter of WHEN to close the connection is something we would need to see your code for.



    logically you would close the connection when no more interaction needs to take place






    share|improve this answer


























    • is he using sockets, is he using Requests?

      – mikeg
      Jan 4 at 16:15











    • was thinking that myself so updated my response! i dont have permissions to simply make comments yet otherwise i wouldn't have actually submitted an "answer" but here we are -.-

      – Brandon Bailey
      Jan 4 at 16:18











    • i am using sockets

      – Dvir
      Jan 4 at 16:35











    • It looks like you're missing socket.close() in some of your while loops. the while loops should run through a series of commands and then close the connection, instead of only closing the connection if they encounter an error. otherwise the connection will stay open until an error is encountered

      – Brandon Bailey
      Jan 4 at 16:42
















    0














    Assuming you're using sock/sockets you can simply run:



    server.quit()


    or



    session.close()


    if you're using requests.



    after creating a server object.



    The matter of WHEN to close the connection is something we would need to see your code for.



    logically you would close the connection when no more interaction needs to take place






    share|improve this answer


























    • is he using sockets, is he using Requests?

      – mikeg
      Jan 4 at 16:15











    • was thinking that myself so updated my response! i dont have permissions to simply make comments yet otherwise i wouldn't have actually submitted an "answer" but here we are -.-

      – Brandon Bailey
      Jan 4 at 16:18











    • i am using sockets

      – Dvir
      Jan 4 at 16:35











    • It looks like you're missing socket.close() in some of your while loops. the while loops should run through a series of commands and then close the connection, instead of only closing the connection if they encounter an error. otherwise the connection will stay open until an error is encountered

      – Brandon Bailey
      Jan 4 at 16:42














    0












    0








    0







    Assuming you're using sock/sockets you can simply run:



    server.quit()


    or



    session.close()


    if you're using requests.



    after creating a server object.



    The matter of WHEN to close the connection is something we would need to see your code for.



    logically you would close the connection when no more interaction needs to take place






    share|improve this answer















    Assuming you're using sock/sockets you can simply run:



    server.quit()


    or



    session.close()


    if you're using requests.



    after creating a server object.



    The matter of WHEN to close the connection is something we would need to see your code for.



    logically you would close the connection when no more interaction needs to take place







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 4 at 16:16

























    answered Jan 4 at 16:15









    Brandon BaileyBrandon Bailey

    449211




    449211













    • is he using sockets, is he using Requests?

      – mikeg
      Jan 4 at 16:15











    • was thinking that myself so updated my response! i dont have permissions to simply make comments yet otherwise i wouldn't have actually submitted an "answer" but here we are -.-

      – Brandon Bailey
      Jan 4 at 16:18











    • i am using sockets

      – Dvir
      Jan 4 at 16:35











    • It looks like you're missing socket.close() in some of your while loops. the while loops should run through a series of commands and then close the connection, instead of only closing the connection if they encounter an error. otherwise the connection will stay open until an error is encountered

      – Brandon Bailey
      Jan 4 at 16:42



















    • is he using sockets, is he using Requests?

      – mikeg
      Jan 4 at 16:15











    • was thinking that myself so updated my response! i dont have permissions to simply make comments yet otherwise i wouldn't have actually submitted an "answer" but here we are -.-

      – Brandon Bailey
      Jan 4 at 16:18











    • i am using sockets

      – Dvir
      Jan 4 at 16:35











    • It looks like you're missing socket.close() in some of your while loops. the while loops should run through a series of commands and then close the connection, instead of only closing the connection if they encounter an error. otherwise the connection will stay open until an error is encountered

      – Brandon Bailey
      Jan 4 at 16:42

















    is he using sockets, is he using Requests?

    – mikeg
    Jan 4 at 16:15





    is he using sockets, is he using Requests?

    – mikeg
    Jan 4 at 16:15













    was thinking that myself so updated my response! i dont have permissions to simply make comments yet otherwise i wouldn't have actually submitted an "answer" but here we are -.-

    – Brandon Bailey
    Jan 4 at 16:18





    was thinking that myself so updated my response! i dont have permissions to simply make comments yet otherwise i wouldn't have actually submitted an "answer" but here we are -.-

    – Brandon Bailey
    Jan 4 at 16:18













    i am using sockets

    – Dvir
    Jan 4 at 16:35





    i am using sockets

    – Dvir
    Jan 4 at 16:35













    It looks like you're missing socket.close() in some of your while loops. the while loops should run through a series of commands and then close the connection, instead of only closing the connection if they encounter an error. otherwise the connection will stay open until an error is encountered

    – Brandon Bailey
    Jan 4 at 16:42





    It looks like you're missing socket.close() in some of your while loops. the while loops should run through a series of commands and then close the connection, instead of only closing the connection if they encounter an error. otherwise the connection will stay open until an error is encountered

    – Brandon Bailey
    Jan 4 at 16:42













    0














    If you are using the Requests library (and you should) you can do this.



    with requests.Session() as session:
    session.get('target_url')


    This will close the connection automatically when everything in the with condition completes.






    share|improve this answer




























      0














      If you are using the Requests library (and you should) you can do this.



      with requests.Session() as session:
      session.get('target_url')


      This will close the connection automatically when everything in the with condition completes.






      share|improve this answer


























        0












        0








        0







        If you are using the Requests library (and you should) you can do this.



        with requests.Session() as session:
        session.get('target_url')


        This will close the connection automatically when everything in the with condition completes.






        share|improve this answer













        If you are using the Requests library (and you should) you can do this.



        with requests.Session() as session:
        session.get('target_url')


        This will close the connection automatically when everything in the with condition completes.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 16:21









        mikegmikeg

        394310




        394310






























            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%2f54042459%2fhttp-proxy-in-python-when-and-how-exactly-should-i-close-connection%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas