#๐Ÿ”’ Help with sending sockets.

16 messages ยท Page 1 of 1 (latest)

runic radishBOT
#

@raven pike

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

upbeat sphinx
#

temp is a file object, not a string.

#

Did you mean to join out, and then encode that?

raven pike
#

its a proxy server and yes

#

oh wait hold on

#
from socket import *
import string
import sys
import socket

#need server socket, bind to port and listen
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    serverSocket.bind(("", 8888))
    serverSocket.listen(5)
    print(f"Listening on {serverSocket.getsockname()}")
except Exception as e:
    sys.exit(f"Failed to bind: {e}")

while True:
    TCPSocket, addr = serverSocket.accept()
    msg1 = TCPSocket.recv(1024)
    
    msg = msg1.decode()
    file = msg.split()[1].partition("//")[2]
    exist = False
    fileToUse = "/" + file.replace("/", "")
    try:
        #see if it's in the cache
        temp = open(fileToUse[1:],"r")
        out = temp.readlines()
        exist = True
        hit = ""
        TCPSocket.send("HTTP/1.1 200 OK\r\n".encode())
        TCPSocket.send("Content-Type:text/html\r\n".encode())
        for i in out:
            TCPSocket.send(i)

    except IOError:
        if exist == False:
            temp1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            host = file.split('/')[0].replace("www.","",1)
            try:
                name = socket.gethostbyname(host)
                print(f"\nTemp help on{name}\n")
                temp1.connect((name, 80))

                fileo = temp1.makefile('rwb', 0)
                fileo.write(("GET http://" + file + " HTTP/1.0\n\n").encode())
                resp = temp1.recv(4096)
                temp3 = ""
                while resp:
                    temp3 += resp.decode()
                    resp = temp1.recv(4096)
                
                if(file[-1:] == '/'):
                    file = file[:-1]
                
                temp4 = open("./" + file.replace("/","") ,"wb")
                temp4.write(temp3.encode())
                temp4.close()
            
                TCPSocket.send(temp3)
            except Exception(e):
                print(f"wrong request")
        else:
            pass
TCPSocket.close()```
#
Traceback (most recent call last):
  File "C:\Users\tsts\OneDrive\Desktop\Homework\proxyserver.py", line 42, in <module>
    temp1.connect((name, 80))
OSError: [WinError 10049] The requested address is not valid in its context

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\t\OneDrive\Desktop\Homework\proxyserver.py", line 60, in <module>
    except Exception(e):
NameError: name 'e' is not defined```
#

I'm just beginning with sockets and servers, I'm not sure where exactly I'm going wrong tbh, but it sorta establishes a connection but then is a pure white page

upbeat sphinx
#

This second error is because except Exception(e) needs to be except Exception as e.

raven pike
#

Oh yeah I noticed but haven't really fixed it yet, I'ma do that right now

#

but the issue I'm facing is that it isn't really loading the google homepage

#

you gotta load it in as a proxy server to actually see it, if that makes sense

#

it makes a new connection and then goes right into a blank page

runic radishBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.