10 months ago
Struggling to convert my python websocket code to go, as it always returns an error 403
import asyncio
import websockets
import os
PORT = int(os.getenv("PORT", 8080))
async def handler(websocket):
await websocket.send("Hello client!")
async def main():
print(f"Listening on port {PORT}…")
async with websockets.serve(handler, "0.0.0.0", PORT):
await asyncio.Future()
asyncio.run(main())
16 Replies
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true // allow all connections
},
}
func handler(w http.ResponseWriter, r *http.Request) {
// Upgrade HTTP to WebSocket
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade error:", err)
return
}
defer conn.Close()
// Send message to client
err = conn.WriteMessage(websocket.TextMessage, []byte("Hello client!"))
if err != nil {
log.Println("Write error:", err)
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", handler)
fmt.Printf("Listening on port %s...\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}not sure what the code does, since I just used AI to generate it.
but you have to use someone's go library,
However, that go library, is actually just a HTTP server with a layer that makes it look like a websocket.
Not actually a websocket, but looks like and acts like it.

10 months ago
Traceback (most recent call last):
File "c:\Users\zacpe\Desktop\PY\---\client2.py", line 10, in
asyncio.run(connect())
~~~~~~~~~~~^^^^^^^^^^^
File "C:\Users\zacpe\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 195, in run
return runner.run(main)
~~~~~~~~~~^^^^^^
File "C:\Users\zacpe\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "C:\Users\zacpe\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py", line 725, in run_until_complete
return future.result()
~~~~~~~~~~~~~^^
File "c:\Users\zacpe\Desktop\PY\---\client2.py", line 6, in connect
async with websockets.connect(uri) as websocket:
~~~~~~~~~~~~~~~~~~^^^^^
File "C:\Users\zacpe\AppData\Local\Programs\Python\Python313\Lib\site-packages\websockets\legacy\client.py", line 633, in __aenter__
return await self
^^^^^^^^^^
File "C:\Users\zacpe\AppData\Local\Programs\Python\Python313\Lib\site-packages\websockets\legacy\client.py", line 650, in __await_impl_timeout__
return await asyncio.wait_for(self.__await_impl__(), self.open_timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\zacpe\AppData\Local\Programs\Python\Python313\Lib\asyncio\tasks.py", line 507, in wait_for
return await fut
^^^^^^^^^
File "C:\Users\zacpe\AppData\Local\Programs\Python\Python313\Lib\site-packages\websockets\legacy\client.py", line 659, in __await_impl__
await protocol.handshake(
...<5 lines>...
)
File "C:\Users\zacpe\AppData\Local\Programs\Python\Python313\Lib\site-packages\websockets\legacy\client.py", line 328, in handshake
raise InvalidStatusCode(status_code, response_headers)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 403import asyncio
import websockets
async def connect():
uri = "wss://sharpengine.smoothcube.net/ws"
async with websockets.connect(uri) as websocket:
msg = await websocket.recv()
print("Server says:", msg)
asyncio.run(connect())If we run localhost on railway, would that be an issue?
cloudflare seems to block the websocket request even though websockets is on
fixed by turning off cloudflare proxy :\
go server now works! <:poggers:720701334659268700>
10 months ago
!s
Status changed to Solved brody • 10 months ago
