AHKhttp - HTTP Server

Post your working scripts, libraries and tools
Skittlez
Posts: 11
Joined: 13 Mar 2014, 12:16

AHKhttp - HTTP Server

16 Oct 2014, 21:02

Basic http server I wrote, requires AHKsock.

Example

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



Source

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus



GitHub
Documentation
Last edited by Skittlez on 08 Nov 2014, 18:53, edited 2 times in total.
User avatar
joedf
Posts: 5649
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: AHKhttp - HTTP Server

16 Oct 2014, 23:27

Interesting
User avatar
fincs
Posts: 500
Joined: 30 Sep 2013, 14:17
GitHub: fincs
Location: Seville, Spain
Contact:

Re: AHKhttp - HTTP Server

17 Oct 2014, 03:24

AutoHotkey on Rails anybody? This little script even supports route-based serving instead of using a folder in disk.
fincs
Windows 10 x64 Build 14393 / AutoHotkey v1.1.24.04
Get SciTE4AutoHotkey v3.0.06.01 - [My project list]
User avatar
joedf
Posts: 5649
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: AHKhttp - HTTP Server

17 Oct 2014, 13:57

Haha true, along with AHK-webkit ;)
User avatar
Relayer
Posts: 98
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: AHKhttp - HTTP Server

18 Oct 2014, 08:47

This looks very interesting but I would like to ask someone to explain a little more how one would use this. It would be very helpful.

Relayer
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

Re: AHKhttp - HTTP Server

18 Oct 2014, 10:42

@Skittlez: do you plan to work on this some more and extend it?

Some documentation would indeed be useful, I've been playing around with a bit - see below.

You can find AHKsock here https://github.com/jleb/AHKsock

Here is a small example displaying parameters and using them in a form. Enter two numbers and press submit will show you the answer on the next page.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Skittlez
Posts: 11
Joined: 13 Mar 2014, 12:16

Re: AHKhttp - HTTP Server

18 Oct 2014, 11:30

I'll write some documentation later today.
@ahk7 If you have any features you'd like to see, just let me know and I'll see what I can do.
guest3456
Posts: 1993
Joined: 09 Oct 2013, 10:31

Re: AHKhttp - HTTP Server

18 Oct 2014, 14:40

awesome

User avatar
Relayer
Posts: 98
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: AHKhttp - HTTP Server

19 Oct 2014, 08:51

ahk7,

I tried your script and get "unable to connect". I'm using Firefox and AutoHotKey v1.1.16.05

I'm a complete noob when it comes to http server stuff.

Relayer
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

Re: AHKhttp - HTTP Server

19 Oct 2014, 12:13

if you google 'unable to connect localhost firefox' you will see that it might be a firefox setting so try it with another browser to see if that works - if it does you can try to figure out how to let firefox allow connections to localhost.
You can test if the server is up and running by opening a command window (cmd.exe) and use ping localhost if it gives valid pings the script (server) works - so you will have to work on getting it to work in your browser. (Edit see comments by Lexikos below)

I'd like to be able to display images in pages and stream mp3 over http - I recall sparrow did that by reading/parsing a list of mimetypes.

Edit: ping but note the test script does a run Run http://localhost:8000/page?para1=123&para2=456 so the default browser should try to open http://localhost:8000 - it may be a firefox problem so worth trying in another browser just to check. Otherwise I don't know why it doesn't work or how to fix it.
Last edited by ahk7 on 21 Oct 2014, 11:20, edited 1 time in total.
guest3456
Posts: 1993
Joined: 09 Oct 2013, 10:31

Re: AHKhttp - HTTP Server

20 Oct 2014, 17:17

Relayer wrote:ahk7,

I tried your script and get "unable to connect". I'm using Firefox and AutoHotKey v1.1.16.05

I'm a complete noob when it comes to http server stuff.

Relayer


Well why don't you tell us what url you are trying to connect to in firefox

The example in the OP uses port 8000 instead of the http default which is 80

lexikos
Posts: 5407
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: AHKhttp - HTTP Server

20 Oct 2014, 21:49

You can test if the server is up and running by opening a command window (cmd.exe) and use ping localhost if it gives valid pings the script (server) works
That will only prove that the computer responds to ICMP (specifically, ping) requests on its loopback interface. It will not prove that the HTTP server is running or that the system will accept TCP/IP connections on any particular port. Some systems don't respond to ping (from outside) but still accept connections on specific TCP ports. I think you'd be hard-pressed to find a system that doesn't respond to ping localhost.

What you can do is attempt to connect to localhost:8000 using telnet or PuTTY, or see if port 8000 is listed by netstat -a.

Of course, that won't be necessary if the problem is as guest3456 guessed.
http://localhost:8000
not http://localhost
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

Re: AHKhttp - HTTP Server

21 Oct 2014, 11:23

I can indeed see it is listening using netstat -a so that is a good tip (edited post above)
kidbit
Posts: 168
Joined: 02 Oct 2013, 16:05

Re: AHKhttp - HTTP Server

27 Oct 2014, 13:28

There's some bug: last 5 symbols of the page get eaten.
ahk7's example results into a page with such a code:

Code: [Select all] [Download] GeSHi © Codebox Plus

<html>
<title>AHKhttp-server 1.0</title>
<body>
<b>para1: 123 - para2: 456<br><br><form action="/calc" method="get">
<input type=text name=para1>
+
<input type=text name=para2>
<input type=submit>
</form></b>
</body>
</
question := (2b) || !(2b) © Shakespeare.
Skittlez
Posts: 11
Joined: 13 Mar 2014, 12:16

Re: AHKhttp - HTTP Server

07 Nov 2014, 22:22

@ahk7 I've added an example of how to serve images.
@kidbit Try updating to the latest version, I can't reproduce that bug.

I'll be adding support for mime types and writing some docs, for real this time, sometime this weekend.
ahk7
Posts: 96
Joined: 06 Nov 2013, 16:35

Re: AHKhttp - HTTP Server

08 Nov 2014, 09:16

Thanks!

I've already been playing around with it and now have a rudimentary mp3 server. I can access the server from other PCs in the network and "stream" MP3 via a downloadable m3u playlist, very nice.

For others who already want to play around with it, you can use the following mimetypes for mp3/m3u

for m3u: (playlists)
res.headers["Content-Type"] := "audio/x-mpequrl"

for mp3:
res.headers["Content-Type"] := "audio/mpeg"

This will play an mp3 in your browser:

Code: [Select all] [Download] GeSHi © Codebox Plus

mp3(ByRef req, ByRef res) {
f := FileOpen(A_ScriptDir . "/groove.mp3", "r") ; example mp3 file
length := f.RawRead(data, f.Length)
f.Close()
res.status := 200
res.headers["Content-Type"] := "audio/mpeg"
res.SetBody(data, length)
}


Look forward to the documentation
Skittlez
Posts: 11
Joined: 13 Mar 2014, 12:16

Re: AHKhttp - HTTP Server

08 Nov 2014, 18:54

Added support for mime types, updated example using ServeFile.
Documentation is on github.
tmplinshi
Posts: 1063
Joined: 01 Oct 2013, 14:57

Re: AHKhttp - HTTP Server

15 Mar 2015, 11:31

Great! Thank you very much!!
tmplinshi
Posts: 1063
Joined: 01 Oct 2013, 14:57

Re: AHKhttp - HTTP Server

17 Mar 2015, 04:44

For anyone who want to use this wonderful lib:
  1. Please use AHK Unicode version in order to work.
  2. ahk7's example code not working? Yes, it's outdated (I tried his code several times, and finally noticed that today...).
    You need make these changes in his code:
    • Replace SetBody to SetBodyText
    • Replace statusCode to status

Thanks for your example code ahk7!

Return to “Scripts and Functions”

Who is online

Users browsing this forum: arcticir and 14 guests